Timers PWM and all that Jazz

Hi Arduino Experts

I'm trying to control LED lights using an Arduino. I have a PWM output switching a MOSFET which is controlling the LEDs - all good so far.

What I now want to do is dim the LEDs when they are on, so rather than having my PWM output on and off, I want the ON periods to be a PWM signal to dim the LEDs and the OFF to be just off.

I'm using the Timer1 library for my actual LED switching as I find I can get far more accurate periods that way.

I've currently got pin 5 of my Nano and pin 9 with the two frequencies I desire and I'm trying to messily AND them together in hardware before applying the signal to the gate of my MOSFET.

There must be a way to do this in software? I'm not clever enough to know how though.

#include <TimerOne.h>

const int PWMPin = 5; // pin 5 contains the driving PWM signal

const int TogglePin = 9; // d9 is switched for on/off

const int DutyCycle = 255;

void setup() {
  pinMode(PWMPin, OUTPUT);  // sets the pin as output
  analogWrite(PWMPin, 10);  // analogWrite values from 0 to 255

  Timer1.initialize(250000);
  Timer1.pwm(TogglePin,DutyCycle);
}

void loop() {

}

Any help or pointers appreciated.

Mark

You want the leds to flash on and off at a preset frequency and duty cycle and with a preset brightness during the “on” period? This using only one Arduino pin to control the leds ?
Which Arduino board have you?

Can’t you just use the pwm to set the brightness , then switch the analog output on and off with a period set by millis , all on the same pin .

Pseudo code

analogWrite(PWMPin, 10);//turn it on
Delay bit;
analogWrite(PWMPin, 0); // turn it off
…and so on, change the “10,0” for a variable , set it to zero when your on/off switch is operated, set it to a value for brightness.

I'm using a Nano

I dont want to use blocking code like delay; in reality there will be all sorts of other things going on.

This image shows what I am trying to generate. The yellow trace is my pin D9, the Blue trace is my pin D5.

The purple is a mathematical AND done by the 'scope, i just want to achieve this in software, without blocking code.

Mark

The solution that @hammy suggested does not block if you use the "Blink without delay" example as a basis:

/*
  Blink without Delay

  Turns on and off a light emitting diode (LED) connected to a digital pin,
  without using the delay() function. This means that other code can run at the
  same time without being interrupted by the LED code.

  The circuit:
  - Use the onboard LED.
  - Note: Most Arduinos have an on-board LED you can control. On the UNO, MEGA
    and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN
    is set to the correct LED pin independent of which board is used.
    If you want to know what pin the on-board LED is connected to on your
    Arduino model, check the Technical Specs of your board at:
    https://www.arduino.cc/en/Main/Products

  created 2005
  by David A. Mellis
  modified 8 Feb 2010
  by Paul Stoffregen
  modified 11 Nov 2013
  by Scott Fitzgerald
  modified 9 Jan 2017
  by Arturo Guadalupi

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/BlinkWithoutDelay
*/

// constants won't change. Used here to set a pin number:
const int ledPin =  LED_BUILTIN;// the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

If you are doing other things in the loop() which may block for a certain time and thus spoil the accuracy of the on/off periods, you can handle the led pin purely through interrupts.

1 Like

Maybe I can use Timer1 to generate an interrupt every period and use the callback to turn on/off my PWM signal?

Now there's an idea!

Mark

bingo, thank you. It was your suggestion about interrupts that got me here:

#include <TimerOne.h>

const int PWMPin = 5; 

const int DutyCycle = 255;

const int PWMOnValue = 10;
const int PWMOffValue = 0;

bool PWMOn = false;

void setup() {
  pinMode(PWMPin, OUTPUT);  // sets the pin as output

  Timer1.initialize(250000); // do I need this?
  Timer1.attachInterrupt(Timer1Callback, 250000);
}

void Timer1Callback()
{
  if (PWMOn == true)
  {
    PWMOn = false;
    analogWrite(PWMPin, PWMOffValue);
  }
  else
  {
    PWMOn = true;
    analogWrite(PWMPin, PWMOnValue);
  }
}

void loop() {

}

OK that works as it is if the duty cycle of the on/off periods is 50% otherwise, you'd have to choose the period of interrupt carefully and maintain a counter in the ISR.
So, for example, the analogWrite(PWMPin, PWMOnValue); statement is executed when the counter is 0, to switch the LED on, and the analogWrite(PWMPin, PWMOffValue) statement is executed when the counter is 8, to switch the LED off, and the counter is reset to 0 when it reaches 11.

This didn't make any sense to me until I figured out that the on/off frequency is 4 Hz and the PWM is 1000 Hz, so

A visibly flashing LED that can be dimmed.

Just in case you slow as me. :expressionless:

a7

1 Like

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