Solutions to the assignment on Buzzer and RGB LED
The RGB LED
The RGB LED consists in fact of 3 tiny LEDs in the basic colors red, green and blue. In the solution program I use the wiringPi initialization call:
wiringPiSetupGpio in which case the GPIO pin numbers are the ones printed on the cobbler. In my case: red: GPIO 17, green: GPIO 18 and blue: GPIO 27.
In the code I use a 3 bit counter (0..7). If bit 0 of the counter is set, I switch red on, if bit 1 is set, green is on, if bit 2 is set blue is on. The LEDs are switched on for 500 ms.
Here is the code:
https://iotworkshop.africa/pub/Embedded_Systems/SolutionsToTheAssignmentLEDAndBuzzer/rgbLed.tar.gz
The Buzzer:
There are 2 different types of buzzers:
- the active buzzer
This one just needs a DC level signal to make noise
- the passive buzzer
This one needs a frequency
The buzzers are connected to GPIO 12 for the active and GPIO 16 for the passive one.
Of course, writing a program for an active buzzer is easier. The program will be exactly the same as we wrote for the (single color) LED.
When trying, my program seemed to fail! There was no sound! Trying with a different buzzer I had recuperated, showed that it was not a software but a hardware problem. The program worked fine on the other buzzer. Finally I found out that the marking on the buzzer PCB was wrong. If I connect the pin marked "S" (for signal) to ground and the one marked "-" (for ground) to the GPIO pin, then everything works fine.
To make things slightly more interesting the
activeBuzzer program sends an SOS.
For the passive buzzer things are slightly more complex as we have to generate a frequency. The
passiveBuzzer program also sends an SOS but this time on a passive Buzzer. Since the passive buzzer takes a frequency, we can actually change the tone. This is done in the
piano program which plays a musical scale. You can easily find the corresponding frequencies on the Internet.
Here is the code:
https://iotworkshop.africa/pub/Embedded_Systems/SolutionsToTheAssignmentLEDAndBuzzer/buzzer.tar.gz
The Light Show
Finally we combine the (passive) buzzer program and the RGB LED program. In
lightShow I play a musical scale and show a different color for each note.
The most interesting (and most complex) program is
song. This one plays a German children song and displays an LED color for each note. The song is coded in an array of structs where each entry contains an index into the frequency table and a duration during which the tone must be played. The program goes through this table until it finds an entry with frequency index=0 and duration=0. In addition the LEDs are lit corresponding to the note played. Try it, its sounds quite cute.
Here is the code
https://iotworkshop.africa/pub/Embedded_Systems/SolutionsToTheAssignmentLEDAndBuzzer/lightShow.tar.gz