Pwm dimming - non linear

i’m trying to accomplish an led perceived Brightness in this waveform holding the peek for 5%of the time or 50ms.

Google Photos

thank you!

OK - you have our permission!

When you have a question, come back!

This looks like a center-aligned PWM with a duty cycle of 5% and a frequency of 1 Hz. Though you can’t get a 20% brightness as you either have 0% or 100%

looks like you need a DAC (not PWM). you need 3 output levels

you could try building one with a resistor voltage divider driven by 2 digital outputs. the output will be 0V if both digital outputs are low and Vcc if both are high. when one is high and the other low, the divided voltage is present

the resistor network could drive a common npn base transistor circuit with an emitter output. the collector connects to Vcc, the emitter will be 0.7V below the base. the voltage at the base from the resistor network controls the emitter voltage, the voltage on the LED.


What I’m trying to accomplish is a pulse in pwm at this wave form pattern.

(If 1000ms 0-255 = linear)

@50ms - 0-40
@150ms - 40-125.
@400ms- 125-255
@400ms-600ms -hold 255
@750ms- 255-125
@1000ms- 40-0

you understand that PWM is On and Off?

What you need is a non-linear mapper.

#include <multiMap.h>
#include <blinker.h>

#define  FADE_CYCLE_TIME   10                      // how many seconds for a full on/off fade?
#define  START_TIME        0                       // ms
#define  END_TIME          FADE_CYCLE_TIME * 1000  // ms
#define  MID_TIME          END_TIME/2              // Ms

#define  FADE_PIN          9        // The fading pin. (Must be pwm out pin)
#define  BLINK_PIN         13       // Any digital pin with an LED attached.
#define  STEP_TIME         50       // Number of mililseconds per change in brightness.

multiMap fadeMapper;                // A non-liner mapper to hold fade over time info.
blinker  LEDBlinker(BLINK_PIN);     // A fire and forget blinker to take care of the blinking LED.
timeObj  frameTimer(STEP_TIME);     // A timer for changing the fade value over time.
int      frame;                     // What step in the fading sequence are we in now?

void setup(void) {

   fadeMapper.addPoint(START_TIME,0);     // Starting at zero
   fadeMapper.addPoint(MID_TIME,255);     // Mid time is at full bright.
   fadeMapper.addPoint(END_TIME,0);       // End time, back to zero.
   LEDBlinker.setOnOff(true);             // Fire up blnkings.
   frame = 0;                             // Starting at frame time zero.

void loop() {

   int fadeVal;
   idle();                                      // Runs baclground stuff like the bliner.
   if (frameTimer.ding()) {                     // If the frame timer has expired (->ding<-)..
      fadeVal = round(;   // Caculate a new fade value by mapping the frame time.
      analogWrite(FADE_PIN,fadeVal);            // Round the fade value to an int and send it out to the PWM pin.
      frame = frame + STEP_TIME;                // Bump up the frame time for the next reading
      if (frame>END_TIME) {                     // If the frame is beyond the last reading..
         frame = 0;                             // Reset it to zero.
      frameTimer.stepTime();                    // Restart our frame timer for the next setting.

This example has three points. You can add as many points as you like. Or as how much ram you have. You will need to install LC_baseTools to compile this.

-jim lee

1 Like

How do I Install that file? Is it just drag and drop the whole thing or do I have to separate the internal files? I’m trying to get 10 things done at once and don’t feel like learning the intricacies at this time,

Copy the entire file folder (LC_baseTools-1.2.4) from the ZIP and Paste it in your Arduino Libraries folder.

No no no nono!! Just go to the menu item “library manager” on the IDE. From there search on LC_baseTools and click install.

Easy peasy.

-jim lee

1 Like

Even easier but just as quick :stuck_out_tongue: thank you both! if either of you know about V2V (vehicle to vehicle wireless comms) please let me know! i have a tread going on for that separately. (using wi-fi to ping and search for other vehicle in the area to sync clock and data)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.