Goal
This week's goal is to add an output device to a microcontroller and program it to do something. Along with Input Devices, this is one of the most importants weeks for me because this is the core of my final project.
hello.servo.44
Since my final project is going to use servos (at least in the first iteration) I am going to try the functionality of the hello.servo.44 board. Unlike other weeks, I swear I will strictly follow spiral development so I will mill and program the board unmodified going through all the steps and later, if I have success and some time left I will start hacking the board.
Milling and stuffing the board
This was supposed to be the easy part of the assignment but I surprised myself that I was unable to do it today, my hands were shaking and it was impossible to me to hold anything with the tweezers. Suddenly, I remembered that I had just drunk an espresso coffee followed by a Coca-cola. Lucky me, Andres Obregon was there helping and holding the pieces for me. Thank you Andres!
I also made a custom connector for the 9V battery using a 6 pin header cable. It isn't my best ever creation, but it's functional.
Understanding how a servo works
A servo motor works by responding to a specific square wave signal, that is, there are only two voltages, zero and pulse voltage V.
The waveform is characterized by the pulse voltage V, the pulse period T, which is 20 ms (50 Hz) in RC servos and pulse width t, that is between 1 ms and 2 ms. When a 1.5 ms pulse width is received, the servo goes to its neutral (not necessarily the center) position. They name it pulse-width modulation or PWM.
Programming the board
I started by uploading the two programs that Neil made using the AVRISP2. The first program hello.servo.44.c uses a dedicated PWM channel on the microcontroller. According to the datasheet, the ATtiny44 has 2 dedicated PWM channels. This makes your life way easier in the software side when dealing with PWM signals. To upload the program to the microcontroller type this in the terminal:
make -f hello.servo.44.make
program-avrisp2
The second program uses software to actually build the PWM signal and hence the program is much more complex, but on the other hand you can use up to five PWM outputs with the ATtinty44 microcontroller. Who gives you more for one dollar?
make -f hello.servo.44.2.make
program-avrisp2
Viewing the PWM signal through the oscilloscope
A few years ago I did not even understand why would someone want to watch those moving waves on a screen. Today I discovered why. When I got the board working I used the oscilloscope to display the PWM signal. At the beginning I tried with an analog 20 Mhz oscilloscope they have here in Barcelona. I love the old style look of the green CRT screen and the knobs. But unfortunately, I could not display the signal like a square. Then I switched to the 150 Mhz digital oscilloscope. To be honest I do not know which button I pressed (I mean just before I saw the signal appear on screen, because I pressed almost all the buttons) but I think it was the blue one that says AUTO SET. Anyway the signal showed on screen and I could clearly see the pulse voltage 5V, and the changing pulse width from 1 ms to 1.5 ms to 2 ms. The oscilloscope is a necessary tool when you start working with electronics at intermediate level (hey maybe I'm not a beginner anymore lol), so I'm now looking for a cheap analog one in ebay.
The result
Since it is difficult to explain what happens with the code I uploaded a video to Vimeo. Here you are the result.
Modifying the board
The challenge starts here. I will add some functionality to the hello.servo board but I am not going to design, mill and stuff another board. As Neil said, when you are developing, all kind of surgery is fine. So get ready to witness really ugly things from now on.
Reducing Jittering (WIP)
While I was looking at the oscilloscope image I saw that the 5V and 0V signals were not a steady line but a shaky one, just as my hand. Later that night I was reading a book named Encyclopedia of Electronic Components from O'Reilly and Make Magazine and found that this is called jittering and happens because since the ground wire of the motor must be common to the ground of the microcontroller, some kind of electronic noise is introduced in the pulse train. A way to reduce it is to place a 0.1uF capacitor bypass between the (red) power wire to the motor and ground. I will do this tomorrow and compare the results in the oscilloscope just before the assignment review.
Controlling a servo with an input device
I didn't say this before, but looking at a servo moving from one position to another like a tennis match in a loop, apart from its questionable utility in real life, is not funny at all. I have a lot of free pins in the microcontroller so what I am going to do now is to read the value from an input device, might be a potentiometer, or maybe a light sensor (I'll decide it tomorrow wednesday) and translate it to a PWM signal. Finally the LDR won the bet and I soldered it to the pin 6 (according this numeration for using with the Arduino IDE).
I coded the new program using the Arduino IDE.
It's a pretty small program that reads the analog input from the
LDR and maps it to new values using the map
function.
But so far I have been unable to make it work properly. I still have to debug the problem. The signal that goes out to the servo is definitely not a PWM signal as you can see in the oscilloscope image below. My best bet is that the map function is not supported by the Arduino IDE when you set the ATtiny microcontroller. It does not complain but I think it is ignoring the map function.
If I have time I would also like to use the piezo that I obtained
from al old electronic device and try to play a version of this
song from a game I used to play when I was young(er). I'm
doing this as a node of next week assignment (networking). 8
bit music forever!
What I learned
I am starting to feel that I can now achieve things that I could not do in the past. But I don't know if this is because I learned something or because as they say here in Barcelona, I am learning to learn.
Download files
You can download all the files related to this week here.