loading

In this tutorial I will show you how to make LED control using Arduino with PWM. For dimming control I will use an encoder. So it will also be a tutorial on how to use the encoder in Arduino projects. In the project I use 6 LEDs each 10W, as a power source I will use a 14.4V battery from a cordless drill. In this guide will use my previous project DIY mega head flashlight with 6x10W LEDs.

This article can also see here:


Arduino PWM LED dimmer - 6x10W LED


Diy LED lights panel for 5$ - Cheap and Easy 6 x 10W

Step 1: Components

  • Arduino UNO
  • LM2596 - step down converter
  • BC547
  • BC557
  • IRF540
  • 2 x 10K ohm
  • 2 x 1K ohm
  • Encoder
  • Mega head flashlight

Step 2: Schematic Diagram

Step 3: Soldering and Mounting

Step 4: Software

This is a piece of code responsible for handling the button encoder.If the button is pressed and the PWM is 100%, then turn off the LEDs. If the button is pressed and the PWM is not 100% then set the PWM to 100%.

bool btn = digitalRead(BTN_PIN);  //Read state of encoder switch.
	  if( btn!=last_button_state && current_time - button_time > 100 ){ //If the state is different from the previous one and since last state change has been at least 100ms.
	    if( btn==LOW && last_button_state ){ //Detect the transitions from HIGH to LOW
	      if( led_power==255 ){
	        led_power = 0;
	      }else{
	        led_power = 255;
	      }
	      analogWrite(LED_PWM_PIN, led_power);
	    }
	    last_button_state = btn;
	    button_time = current_time;
	  }
This code fragment is responsible for handling encoder. It recognizes whether the encoder is rotated left or right. If left, reduce the PWM value. If right it increases the PWM value.
if( current_time - loop_time >= 5 ){
    encoder_A = digitalRead(EN_PIN_A);  //Read encoder pin A
    encoder_B = digitalRead(EN_PIN_B);  //Read encoder pin B
    
    if( !encoder_A && last_encoder_A ){
      if( encoder_B ){
        Serial.println( "L: " + String(led_power) );
        led_power = led_power - power_step;
      }else{
        Serial.println( "R: " + String(led_power) );
        led_power = led_power + power_step;
      }
All dimmer software.
#define LED_PIN 13
	#define LED_PWM_PIN 11
	
	#define EN_PIN_A 12
	#define EN_PIN_B 8
	#define BTN_PIN 4
	
	
	unsigned char encoder_A;
	unsigned char encoder_B;
	unsigned char last_encoder_A;
	unsigned char last_encoder_B;
	int led_power = 0;
	int power_step = 1;
	long loop_time;
	long button_time;
	bool last_button_state;
	
	void setup() {
	  pinMode(LED_PIN, OUTPUT);
	  pinMode(EN_PIN_A, INPUT_PULLUP);
	  pinMode(EN_PIN_B, INPUT_PULLUP);
	  pinMode(BTN_PIN, INPUT_PULLUP);
	  
	  analogWrite(LED_PWM_PIN, 0);
	
	  Serial.begin( 115200 );
	  Serial.println( "START" );
	}
	
	void loop() {
	  long current_time = millis(); //millis() - Returns the number of milliseconds since the Arduino board began running the current program.
	
	  bool btn = digitalRead(BTN_PIN);  //Read state of encoder switch.
	  if( btn!=last_button_state && current_time - button_time > 100 ){ //If the state is different from the previous one and since last state change has been at least 100ms.
	    if( btn==LOW && last_button_state ){ //Detect the transitions from HIGH to LOW
	      if( led_power==255 ){
	        led_power = 0;
	      }else{
	        led_power = 255;
	      }
	      analogWrite(LED_PWM_PIN, led_power);
	    }
	    last_button_state = btn;
	    button_time = current_time;
	  }
	  
	  if( current_time - loop_time >= 5 ){
	    encoder_A = digitalRead(EN_PIN_A);  //Read encoder pin A
	    encoder_B = digitalRead(EN_PIN_B);  //Read encoder pin B
	    
	    if( !encoder_A && last_encoder_A ){
	      if( encoder_B ){
	        Serial.println( "L: " + String(led_power) );
	        led_power = led_power - power_step;
	      }else{
	        Serial.println( "R: " + String(led_power) );
	        led_power = led_power + power_step;
	      }
	
	      if( led_power < 0 ) led_power=0;
	      if( led_power >= 255 ) led_power=255;            
	
	      if( led_power >= 0 && led_power <=10 ){
	        power_step = 1;
	      }else if( led_power > 10 && led_power <=20 ){
	        power_step = 2;
	      }else if( led_power > 20 && led_power <=30 ){
	        power_step = 5;
	      }else if( led_power > 30 ){
	        power_step = 10;
	      }
	      
	      analogWrite(LED_PWM_PIN, led_power);
	    }
	
	    last_encoder_A = encoder_A;
	    last_encoder_B = encoder_B;
	   
	    loop_time = current_time;
	  }
	}
Download source code: Mega_head_flashlight.ino

Step 5: The Project Is Finished

After assembling all parts together and uploading the software, it's time to test.


About This Instructable

205views

6favorites

License:

Bio: Software, electronics, do-it-yourself
More by adachsoft:Homemade Laptop Table for Bed Raspberry PI With DS18b20 Web Server PHP - Remote Temperature Measurement Arduino PWM LED Dimmer - 6x10W LED 
Add instructable to: