🔍

Basic LED helper for Arduino & ESP32

Arduino LED Helpers

Simply create an LED object from the helper functions to handle fading, pulsing or manual call of the LED without interfering with the main loop routines.


// ----------------------------------
// -------- MAIN LOOP  --------
// ----------------------------------


// setup the LED object  aLED namewhatever(pin);
aLED led1(11);

void setup() {
}

void loop() {
 
  led1.operateLED(); //operate specific LED
  handleSerial(); //only for testing purpose!!!
  
  delay(20);
}


// very basic function to handle serial command from pc console
void handleSerial() {
  while (Serial.available() > 0) {

       char incomingCharacter = Serial.read();
       switch (incomingCharacter) {

        // set led to manual mode
        case 'm':
          led1.setMode(0);
          led1.brightness = int(random(0,255) );
        break;


         // set led to pulse mode
        case 'p':
        led1.setMode(1);
        led1.pulse_speed  = ( random(0,1) *.4) + .01; // set the pulse speed
        led1.pulse_amplitude  = int(random(10,255)); // set the pulse amplitude
        break;


         // set led to fade mode
        case 'f':
        
        led1.fade_target = int(random(0,255)); // set a random fading target 0-255
        led1.fade_speed = int(random(4,20)); // set a random fading speed > 1
        led1.setMode(2);
       
        break;


        
       }
  
  }
  
 }


This helper code needs to be PRIOR to your mainloop – or 🙁

// ----------------------------------
// -------- BASIC LED HELPER --------
// ----------------------------------

// basic lerp function
float return_lerp(float _s, int _target,int _time){
   _s = _s + (( float(_target) - _s)/float(_time));
   return _s;
    
}

// --------------
// LED Object
// -------------
 class aLED {
  private:
    byte pin;
    long ts = 0;
    int led_mode = 0; //0 manual / 1:sinpulse / 2:fading
    
   
   public:
   int brightness = 0; // current brightness
   float fade_target = 0; // where to fade?
   int fade_speed = 4; 
   float pulse_speed = .004; // speed of pulse when in pulsemode
   int pulse_amplitude = 125;
    
    aLED(byte pin) {
     this->pin = pin;
     init();
    } 

    void init(){
        pinMode(pin,OUTPUT);
    }

    void operateLED(){
        if(led_mode==0){ 

        // if in manual mode
         
        }else if(led_mode==1){
          
          // -----------  PULSE MODE
          brightness =  int((sin(millis()*pulse_speed)+1)*pulse_amplitude*.5);
          
        }else if(led_mode==2){
          
          // -----------  FADING MODE
         brightness = int(return_lerp(brightness,fade_target,fade_speed));

        }
      
      analogWrite(pin,brightness);

    
    }

    void setMode(int _mode){
        led_mode = _mode;
     }
    
};

ESP32 LED Helpers

The ESP32 has a slightly different architecture than Arduino – so the PWM is managed differently. The following code works for ESP32 with the same features as Arduino.

// ----------------------------------
// -------- MAIN LOOP  --------
// ----------------------------------


// setup the LED object  aLED namewhatever(pin);
aLED led1(12);
aLED led2(14);



void setup() {


  
  Serial.begin(9600);
}

void loop() {
 
  led1.operateLED(); //operate specific LED
  led2.operateLED(); //operate specific LED
  handleSerial(); //only for testing purpose!!!
  
  delay(20);

  
}


// very basic function to handle serial command from pc console
void handleSerial() {
  while (Serial.available() > 0) {

       char incomingCharacter = Serial.read();
       switch (incomingCharacter) {

        // set led to manual mode
        case 'm':
          led1.setMode(0);
          led1.brightness = int(random(0,255) );
        break;


         // set led to pulse mode
        case 'p':
        led1.setMode(1);
        led1.pulse_speed  = ( random(0,10) *.4) + .01; // set the pulse speed
        led1.pulse_amplitude  = int(random(10,255)); // set the pulse amplitude

         led2.setMode(1);
         led2.pulse_speed  = ( random(0,10) *.4) + .01; // set the pulse speed
        break;


         // set led to fade mode
        case 'f':
        
        led1.fade_target = int(random(0,255)); // set a random fading target 0-255
        led1.fade_speed = int(random(4,20)); // set a random fading speed > 1
        led1.setMode(2);
       
        break;


        
       }
  
  }
  
 }
// ----------------------------------
// -------- BASIC LED HELPER --------
// ----------------------------------

// basic lerp function
float return_lerp(float _s, int _target,int _time){
   _s = _s + (( float(_target) - _s)/float(_time));
   return _s;
    
}

// ONLY FOR ESP32 needed!
// setting PWM properties
const int freq = 5000;
const int resolution = 8;
// NOTE : ESP32 can only manage 16 diferent PWM channels 
// https://circuitdigest.com/microcontroller-projects/esp32-pwm-tutorial-controlling-brightness-of-led


// --------------
// LED Object
// -------------
 class aLED {
  private:
    byte pin;
    long ts = 0;
    int led_mode = 0; //0 manual / 1:sinpulse / 2:fading
    int myledChannel = int(random(15));
   
   public:
   int brightness = 0; // current brightness
   float fade_target = 0; // where to fade?
   int fade_speed = 4; 
   float pulse_speed = .04; // speed of pulse when in pulsemode
   int pulse_amplitude = 222;
    
    aLED(byte pin) {
     this->pin = pin;
     init();
    } 

    void init(){
       // pinMode(pin,OUTPUT);
        
           // configure LED PWM functionalitites
          ledcSetup(myledChannel, freq, resolution);
          ledcAttachPin(pin, myledChannel);
         
        // attach the channel to the GPIO to be controlled
        
        
    }

    void operateLED(){
        if(led_mode==0){ 

        // if in manual mode
         
        }else if(led_mode==1){
          
          // -----------  PULSE MODE
          brightness =  int((sin(millis()*pulse_speed)+1)*pulse_amplitude*.5);
          
        }else if(led_mode==2){
          
          // -----------  FADING MODE
         brightness = int(return_lerp(brightness,fade_target,fade_speed));

        }
      
     //digitalWrite(pin,brightness);
      ledcWrite(myledChannel, brightness);
      //analogWrite(pin,brightness); >>> WORKS for ARDUINO!!!!
      

    
    }

    void setMode(int _mode){
        led_mode = _mode;
     }
    
};