create new tag
view all tags

Solutions to the assignment on Buzzer and 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:


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:


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


-- Uli Raich - 2018-11-15


Topic attachments
I Attachment History Action Size Date Who Comment
Unknown file formatgz buzzer.tar.gz r1 manage 8.9 K 2018-11-15 - 16:48 UliRaich  
Unknown file formatgz lightShow.tar.gz r1 manage 1.3 K 2018-11-15 - 16:48 UliRaich  
Unknown file formatgz rgbLed.tar.gz r1 manage 0.7 K 2018-11-15 - 16:48 UliRaich  
Edit | Attach | Watch | Print version | History: r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r2 - 2018-11-16 - UliRaich
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2021 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback