Changing duty cycles for Multi-color LED without AnalogWrite

Hello everyone,

I'm quite new to programming for Arduino Uno and I need some help.... :sweat_smile:
I would like to programm a code, where I can adjust the duty cycle (from terminal) for RGB LED using PWM and Timer Overflows (without using AnalogWrite). I've looked up on the internet for many tutorials on how to work with Timers etc, but I'm still very lost.

Is there any way you could help me, please?

Thanks for any suggestions.

These two statements contradict each other:

That relatively advanced coding for a newbie. A class assignment perhaps? If so, the two places to start are the material your instructor has already covered and a study of the ATMega328P datasheet.

1 Like

Yep, it is a class assignment. I studied the datasheet, but I'm still hopeless and don't understand it since this is the first time I'm working with this part of programming for Arduino. The material provided by my lecturer has less info than the datasheet, so that doesn't help me neither...

The tip - see the source code of the analogWrite

Many times instructors don't spoon-feed you the exact material that will be covered in later assignments and exams. They do however provide you with the basic information and tools that you need to expand into new areas from the information that is covered. That's how education works. It's been that way for every class I've ever taken, all the way through Graduate School.

Have you studied Nick Gammon's Article on Timers and Counters?

presumably you understand that a PWM pulse is simply on a fraction of a period.

so if you define some period, P, and some fraction of a period, F (< 1), then the on time is PF and the off time is P(1-F) where these two times can be implemented with a suitable delay (e.g delayMicroseconds())

Without analogWrite, you have two choices…

HARDWARE PWM, using the processor’s internal timers & counters, In this case, usually set up using the analogWrite function from the Arduino core libraries.

—or—

SOFTWARE PWM, creating and maintaining your own timers and counters in C/C++.

The latter imposes a greater load on the cpu, because the software is changing states at a high speed, while hardware peripherals can offload most of that burden to ‘hardware’.

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