Ceci est une ancienne révision du document !
Correction: In the previous article I compared the compiled file sizes, the given sizes are in Byte, not in KByte. My apologies for this inaccuracy.
In the last issue I introduced you to the software and hardware pulse width modulation (PWM) to dim a LED. And in the first examples we needed not much hardware than the microcontroller, a resistor and a LED. And, in these first few examples the only hardware/components we need are a microcontroller, a resistor and an LED.
This time I will show you how you can set the brightness of the LED or the speed of the PWM by controlling it with a potentiometer. For a good explanation of a potentiometer see: https://en.wikipedia.org/wiki/Potentiometer
In the previous example we learned that a microcontroller can understand if a switch (or any other device) is ‘ON’ (operating at between 3/5V) or ‘OFF’ (operating at 0V) because of its digital nature, but how will the microcontroller know that a voltage is somehow in between (e.g. at 2.3 Volts)? We can use the inbuilt analog to digital conversion of the attiny13a. Besides controlling the microcontroller within the source code, this will be the first time the microcontroller can be controlled from the outside world.
Analog to digital conversion
The attiny13a features a 10-bit successive approximation analog to digital conversion (ADC), which means, that the input voltage is repeatedly compared to a reference voltage until the input and the reference voltage are nearly equal. The attiny13a has 4 multiplexed ADC channel (PIN PB2-5), each one can be used independently to measure voltages. For the moment we leave out some special functions such as free running mode, start ADC on interrupt or trigger an interrupt after conversion. The datasheet of the attiny13a has detailed information about this topic, but Great Cow BASIC has got you covered, so that using the ADC can be done quite easily.
For the actual purpose I will use the single conversion mode of the ADC at the 8-bit resolution. This is precise enough, because the duty cycle of the PWM has the same resolution of 8-bit (values range from 0 - 255) so I do not need to map a higher to a lower resolution. Second I will use a medium frequency to measure the voltage; the higher the frequency the measurement takes place the more inaccurate the readings are. In 8-bit resolution every ADC value corresponds to a voltage level (ADC value = Volt255). If the microcontroller is powered at 5 V the measurement would read around 0.0196 mV per step. Powering it at 3 V the readings for each step would be around 0.0117 mV. See the table below for some arbitrary values.
Controlling the LED
To control the LED with the potentiometer I will expand the software PWM code to read the analog value of the potentiometer and set the brightness accordingly.
This works of course with the hardware PWM variants of the previous article, I have not included the PWM variants out to streamline this article.
Breadboard circuitry
I assume that you flashed the program to the microcontroller with your preferred programmer or an Arduino as ISP. To try out the program now place the attiny13a somewhere in the middle of a breadboard so that the upper and the lower side are placed above and under the middle line of the breadboard.
I prefer to have PIN 8 (VCC) on the upper left hand corner of the breadboard. The potentiometer should have three terminals, place the potentiometer: the terminals are in front of you and begin to connect from left to right: first terminal should go to ground, second to PIN 3 (PB4) of the microcontroller and third terminal should go to positive supply voltage (VCC). If the terminals are not properly connecting to the breadboard solder some wires to the terminals or stick a male / female jumper wire to them and connect the wires to the breadboard (if you have another form of potentiometer please refer to the appropriate datasheet). Then connect the anode (long lead) of the LED through a 220 Ohm resistor with PIN 6 (PB1) and the cathode (short lead) of the LED to Ground. After this connect the power supply and see how the brightness of the LED changes by turning the potentiometer left or right.
Conclusion
Reading analog values is a very useful function of the microcontroller and there are many possible uses for it. Besides reading a potentiometer you could e. g. measure the systems battery status and implement a draining protection for your systems battery. As an exercise you could expand the code to not only control the brightness of the LED but also the length of the pauses. In the next article we will experiment further with the analog digital conversion and see if we can print out the measured values to the serial console.
References
In depth explanation of the ADC http://maxembedded.com/2011/06/the-adc-of-the-avr/
ADC code optimisation http://gcbasic.sourceforge.net/help/_analog_digital_conversion_code_optimisation.html
Acknowledgement
I wish to thank Evan Venn (Anobium) from the Great Cow BASIC Team for his insights and valuable hints.
Also thanks to Bernd Dau for the hint with wrong file sizes.