PWM Dimming Control using RCD-24-1.20

Hi people, i’m a new user to Arduino. I am currently facing a problem here. I am using a Arduino Uno with a AtMega328P. I am trying to create a code that can change a duty cycle while maintaining a fix frequency. I am aware that Arduino could produce its own frequency and i’m trying to make use of that as i would not like to use a frequency from an outside source.

The attached diagram is the circuit that i am using. I would attach the Arduino Uno to the TTL level PWM signal. The circuit would then light up the LED using the Arduino to control the brightness.

Is there any particular reason not to use PWM. Seems you can just PWM this module with Arduino's default PWM frequency. No caps, no high frequency. Leo..

I am not sure why you would want to drive this with high frequency pwm through the analog input as indicated in your jpeg attachment.

From the RCD-24-1.20 data sheet

Maximum PWM Frequency For Linear Operation 20 -200Hz

The default pwm frequency from the Arduino is either 490 or 980 hz depending on the output pin used. It is a very simple one line of code to change the prescaler of the timer and modify the default pwm frequencies. In your case, I would recommend that you use either digital pin 9 or 10 which come from timer 1 or pins 11 or 3 which come from timer 2 and change the default prescaler of 64 to 256 which would lower the pwm frequency to 490/4.

Of course you could change the prescaler the other way to get faster pwm cycles required to drive the analog, but you might want to use a library for Timer1 as the setup for the high frequencies is not so simple, unless you want slightly above 31K which you can get by dropping the prescaler from 64 to 1. But as Wawa says, you may be asking for trouble with this route.

Confusing datasheet. They mention 200hz max dim frequency and 1000hz max dim frequency. I think there is already some RC filtering done on the ANALOGUE input, so on that input max frequency is 200hz. And max PWM frequency on the PWM input is 1000hz. Arduino's default ~500hz PWM frequency should be no problem. No special code. Just analogWrite a value of 0-255 to the pin. See the fading example in the IDE. Leo..

Hi cattledog,

Thank you for the reply. How do i change the default prescalar? Could you also explain how the timer works? From what i know is that you have ti use TCCRnB whereby replacing the 'n' to the timer number you want to use. But i aren't very sure how to do the coding. If you could give me an example & explanation on how it works, i would definitely appreciated it very much! Thank you very much for your help.

Hi Wawa,

Thank you for the reply and looking into the datasheet. Is there any specific pin to use if i were to use the PWM? As i know there is 3 timer to choose from. And as for the program do i just write [analogWrite(255);]? And 255 is for 100% duty cycle right? If you dont mind could you write out an example of a program code as I do not understand PWM well. I would definitely appreciated it very much! Thank you very much for your help.

Here is a basic example which puts 122 Hz pwm from Timer 2 on pin 11. I have added code so that if you jumper pin 11 to pin 2, you can read the pwm frequency with an interrupt.

There is a good tutorial on timers here Gammon Forum : Electronics : Microprocessors : Timers and counters
and here
https://www.arduino.cc/en/Tutorial/SecretsOfArduinoPWM

When you start using timers, you are entering “data sheet territory” and you may wish to stay with the default PWM frequency of 490 hz on pins 3,11 (Timer 2) or pins 9,10(Timer 1). Timer 0 has pwm output on pins 5 and 6 at 980Hz. The data sheet for the RCD-24-1.20 indicates that 490 hz should work but may be non-linear. Wawa has alot of hardware experience and he indicates it should work with default PWM if you don’t want to get into changing Timers.

volatile unsigned long  count = 0;
unsigned long copyCount = 0;
unsigned long lastRead = 0;
unsigned long interval = 1000;

void isrCount()
{
  count++;
}

void setup()
{
  Serial.begin(115200);
  Serial.println("starting...");

  attachInterrupt(0, isrCount, RISING); //interrupt on pin 2

  TCCR2B = 0;//initialize register
  TCCR2B |= 1 << CS21 | 1 << CS22; // set Timer2 prescaler 256

  pinMode(11, OUTPUT); // pwm pin 11 -> jumper to pin 2
  analogWrite(11, 127);//50% duty cycle
}

void loop()
{
  if (millis() - lastRead >= interval) //read interrupt count every second
  {
    lastRead  += interval;
    noInterrupts();
    copyCount = count;
    count = 0;
    interrupts();
    Serial.println(copyCount);
  }
}

The module has an analogue pin. 0volt = 100% brightness and ~4.25volt is 0% brightness. It seems default is 0volt (ground), so don't connect.

And a PWM pin that could be use for on/off if you would be using analogue control.

Just connect PWM pin3 and ground pin4 to one of Arduino's ~490hz PWM pins, and use analogWrite. Google "arduino pwm frequency". It seems the module is using reverse logic, so full brightness = 0, and 255 is off. Leo..