Fast Rainbow Circuit
Three PWM Signal w/ ATtiny85 #arduSerie 30
Getting 3 PWM from ATtiny85 and drive RGB LED in a round-fashion mode like rainbow is not a trivial task.
Hi, Hobbyists!
This is the Fast Rainbow Circuit! (codes on github repo)
Let’s jump straight in the codes! Here they are:
The first thing to be done is to record on ATtiny85. For this, take these first two steps below. You can find these steps explained in this post or this video. it’s pretty easy. But anyway, here is the step by step:
Task 00 — Upload ArduinoISP to your board, as usual (here I’m using Arduino 1.8.2)
Task 01 — Prepare your breadboard like this schema
Task 02 — Now upload the code below to your ATtiny85 — this video can guide your effort (realize that this time I’m using Arduino 1.6.4).
Make some change in your breadboard. Add RGB LED and some resistors… Set the first schema above.
As result your RGB LED should be cycling in a rainbow color round! see video.
Task 03 — Now the real fun begins:
We will setting Arduino as a data collector so in the end we must get this graph:
See result of combining colors as they blend into each other. Red has lower ttl levels. See multimeter experiment below.
This is the code for collecting data from Arduino 1.8.2:
Back to main Codes:
Now let’s comment this code (3 PWM):
https://github.com/giljr/Ardu_Serie/blob/master/_30_arduSerie_ATTiny85_three_PWM_output.ino
But, first, let’s understand how to generate a PWM on a pin, let’s say, PB0.
As you may know there are three modes of operation of PWM Timers:
Fast PWM
Phase Correct PWM
Frequency and Phase Correct PWM
Lets stick with Fast PWM.
In simple terms, this is Fast PWM! In this mode, since sawtooth waveform is used, the timer counter TCNTn (n = 0,1,2) counts from BOTTOM to TOP and then it is simply allowed to overflow (or cleared at a compare match) to BOTTOM.
In this post I am publishing the code for three pwm. In my github you find the whole series, from 1 to 4 pwm signals.
Check it out if you think it matters!
Would you please open ATtiny85 datasheet and bear with me.
For getting 3 PWM from ATtiny85, these are the registers you have to deal with:
Index: DDRB TCCR0A TCCR0B TCCR1 GTCCR
DDRB — Port B Data Direction Register
DDRB |= (1 << DDB4) | (1 << DDB1) | (1 << DDB0);
Port B Data Direction Register (controls the mode of all pins within port B)
DDRB is 8 bits: [unused:unused:DDB5:DDB4:DDB3:DDB2:DDB1:DDB0]
1<<DDB4: sets bit DDB4 (data-direction, port B, 4th pin), which puts pin 3 on ATtiny85 in output mode, color Blue;
1<<DDB1: sets bit DDB1 (data-direction, port B, 1th pin), which puts pin 6 on ATtiny85 in output mode, color Green;
1<<DDB0: sets bit DDB0 (data-direction, port B, 0th pin), which puts pin 5 on ATtiny85 in output mode, color Red (Item 10.4.3 datasheet);
TCCR0A — Timer/Counter Control Register A
TCCR0A = 3 << COM0A0 | 3 << COM0B0 | 3 << WGM00;
Control Register A for Timer/Counter-0 (Timer/Counter-0 is configured using two registers: A and B)
TCCR0A is 8 bits:[COM0A1:COM0A0:COM0B1:COM0B0:unused:unused:WGM01:WGM00]
3<<COM0A0: sets bits COM0A0 and COM0A1, which (in Fast PWM mode) sets OC0A on compare-match, and clear OC0A at BOTTOM — inverting mode;
3<<COM0B0: sets bits COM0B0 and COM0B1, which (in Fast PWM mode) sets OC0B on compare-match, and clear OC0B at BOTTOM — inverting mode (Table 11–3 datasheet);
3<<WGM00: sets bits WGM00 and WGM01, which (when combined with WGM02 from TCCR0B below) enables Fast PWM mode (Table 11–5 datasheet).
TCCR0B — Timer/Counter Control Register B
TCCR0B = 0 << WGM02 | 3 << CS00;
Control Register B for Timer/Counter-0 (Timer/Counter-0 is configured using two registers: A and B)
TCCR0B is 8 bits: [FOC0A:FOC0B:unused:unused:WGM02:CS02:CS01:CS00]
0<<WGM02: bit WGM02 remains clear, which (when combined with WGM00 and WGM01 from TCCR0A above) enables Fast PWM mode (Table 11–5 datasheet);
3<<CS00: sets bits CS00 and CS01 (leaving CS02 clear), which tells Timer/Counter-0 to use a prescalar 64 (Table 11–6 datasheet).
TCCR1 — Timer/Counter1 Control Register
TCCR1 = 0 << PWM1A | 3 << COM1A0 | 7 << CS10;
TCCR1 is 8 bits: [CTC1:PWM1A:COM1A1:COM1A0:CS13:CS12:CS11:CS10]
0<<PWM1A: bit PWM1A remains clear, which prevents Timer/Counter-1 from using pin OC1A (which is shared with OC0B) pin 6;
3<<COM1A0: sets bits COM1A0, COM1A1 compare match with compare register A in Timer/Counter1 — OC1A (Table 12–1 datasheet);
7<<CS10: sets bit CS10, CS11 and CS12 which tells Timer/Counter-1 to use a prescalar of 64 ( Table 12–5 datasheet).
GTCCR — General Timer/Counter1 Control Register
GTCCR = 1 << PWM1B | 3 << COM1B0;
GTCCR is 8 bits: [TSM:PWM1B:COM1B1:COM1B0:FOC1B:FOC1A:PSR1:PSR0]
1<<PWM1B: sets bit PWM1B which enables the use of OC1B on pin 3
3<<COM1B0: sets bit COM1B0 and COM1B1, which (when in PWM mode) sets the OC1B output line; note that the corresponding direction control bit must be set (one) in order to control an output pin ( Item 12.3.2 datasheet).
With the choice of prescale we will calculate the frequency of the pwm signal. Look this formula:
fpwm = fclk / N * 256
where fpwm is the frequency of PWM, fclk is the frequency of ATtiny’s clock and N is prescale. We choose 64, hence:
f = 1.000.000 / 64 * 256
f = 61,03515625 Hz
See on the SoundCard Oscilloscope:
Finally, update compare registers with red, green and blue values, mixing colors two by two in a well-paced fashion and finally a very brief pause so we can see all dacing collors
for (int i=0; i <= 255; i++) {
SetColour((colour+1) % 3, i);
SetColour(colour, 255-i);
delay(1);
}
Mixing colors two by two in a well-paced fashion. See the chart above to understand the move!
Now let’s take some measurements with the multimeter:
For calculation of the ideal resistor, here is the formula:
R = Vsupply — Vfind / If
Where Vsupply — voltage supply, Vfind — ideal voltage for LED and If — ideal current:
R = 5–1,63 / 10 * 10^-3
R = 337 Ohms
Here is what I’ve got:
Resitors 220 R
R 1v76 17,3mA
G 2v73 17,2mA
B 2v69 16,7mA
Resistors 330 R
R 1v63 11,86mA
G 2v61 11,86mA
B 2v64 11,63mA
So this is it! In the next post, We will return to the implementation of our videos on proteus, this time we will work with the electronic board.
So till then, catch up with me by posting your comments in my youtube channel!
And don’t forget to thumbs it up;-)Enjoy!
Download all the Project Archives
References & Credits:
Four PWM Outputs from the ATtiny85 by technoblogy.com
Activity: Diode current vs. voltage curves by wiki.analog.com/university
Human Vision and Color Perception by www.olympus-lifescience.com
AVR Timers — PWM Mode — Part by maxembedded.wordpress.com
ATtiny PWM (updated) by matt16060936.blogspot.com
ATtiny datasheet by microchip.com
Soundcard Scope — by: Christian Zeitnitz
Video tutorial by j3