LED Fading with Timer1

Hello guys,

Im fairly new with the Arduino and we have to program something I just can’t get my head around it.
I hope you can help me.
So I want a LED to constantly increase and decrease in brightness with a sinus signal with 0,5Hz
We are supposed to use The Timer1 for that matter with prescaler 1 16bit and FASTPWM.
We have to use the Registers themselves and not the Analogwrite function.

I hope you can help me out.

I suggest to make a start using analogWrite(). Once you have that working, look at achieving the same thing programming timer1 directly.


Yes , so I have already done it with the analogWrite() function.
Thats no problem.
The problem is transferring the Analogwrite() function into directly working with the registers.
to get a sinus signal I've been using this formula 127sin(2PI*time/period) for time i used mills() and for period the 2seconds, but how can i transfer this to directly working with the registers

Its basically just about using the timer1 registers. i can't seem to figure out a way to set it up to give me that sinus signal as analog output.
Thanks for any help.

Post the code you have already got.
That formula gives you the desired brightness of the led as a specific point in the 0.5 Hz cycle ?
Is that correct ?
Then, in other words, it relates to the duty cycle of the led at that point (%time on).
So at discrete intervals, you have to change the duty cycle of the timer. The shorter the intervals, the more progressive (smoother) the change in brightness will be.

You find the description of the timer in that datasheet for the processor you are using, say an ATMEGA328P for an Arduino Uno. In the datasheet I have, the chapter is "16-bit Timer/Counter1 with PWM"

By it self, it can't. The hardware of Timer1 will just give you a stay PWM signal (if you set it to fastPWM) and don't change anything. Aka, the whole assignment is the think of what you need to do in order to make a (Pulse Width) modulated sin out of it. It's simply not a set-and-forget thing :slight_smile:


Ah yes, it is fast pwm and is not so flexible.
One way, I'm not saying it is the best, is with home made PWM with say 100us ticks and a tick counter which rolls over at 100. So for a 25% brightness, the Led has to be on at tick 0 and off at tick 25. If there is too much flicker in the periods of low duty cycle, reduce the tick counter rollover.
This requires an Interrupt service routine in which the tick counter is incremented and the led is switched on and off.