Showing posts with label piezo buzzer. Show all posts
Showing posts with label piezo buzzer. Show all posts

Sunday, February 6, 2022

Electronic Birthday Card

For my friend's birthday, I made her a theater-themed electronic card that sings happy birthday. The outside looks like a theater announcement, and the inside looks like a movie marquee. My girlfriend printed both graphics on a Cricut machine, and they look much better than what I could have come up with myself.

The hardware is pretty simple. The top part, which flips up to show the marquee, is a simple 3D-printed box. My original plan was to use the box as the bottom part to hold the battery and electronics. After waiting six hours for the part to print, I realized it wasn't tall enough to hold the LEDs of the marquee, so the part became the lid instead of the base. For the bottom part, I cut a piece of plexiglass from the hardware store and printed standoffs to attach it to the circuit board. This was my first time working with plexiglass, which turned out to be very easy to crack or break while working with. It's neat to have some experience with it in case another project needs a plexiglass case.

The movie marquee inside has 20 LEDs in total around the edge. They are arranged in five groups of four LEDs with an NPN transistor controlling each group. The groups are interspersed so that each fourth LED is in the same group and shares the same brightness. The blinking pattern rotates clockwise with the brightest LEDs in the front of the pattern so the other LEDs look like they are fading and trailing the brightest one. This looks better than just lighting up one group at a time. The brightness is controlled by simple software PWM running on an MSP430. A timer fires every 1,000 cycles giving 16,000 steps per second since the microcontroller runs at 16MHz. Every time through the loop, the code to adjust the PWM executes then blocks on a while loop waiting for the timer to finish it's 1,000 cycles. This keeps the brightness uniform since a step always takes the same amount of time regardless of how long the PWM code needs.

16,000 steps per second is way more than needed to adjust LED brightness. The extra granularity is useful for controlling the small piezo buzzer that plays happy birthday. The music consists of simple beeps from the speaker, which only takes a few dozen bytes to store and has no problem fitting in the MSP430's flash. The music notes start out as raw frequency values beginning with A at 440Hz. A series of C macros let me calculate how many steps each note needs including a short bit of silence at the end of each one. While I try to avoid overreliance on macros, in this case they really helped list everything in a clear way. Specifying the notes to be played was then very easy:

const unsigned int music_notes[]={

    NOTE(NOTE_C,EIGTH),
    NOTE(NOTE_C,EIGTH),
    NOTE(NOTE_D,QUARTER),
    NOTE(NOTE_C,QUARTER),
    NOTE(NOTE_F,QUARTER),
    NOTE(NOTE_E,HALF),
    ...
    MUSIC_END};

My first try at playing notes in a scale was noticeably off, and two of the notes had the same tone. It took some investigating to figure out that the way I had the timer calculations listed based on the frequency was rounding the highest frequency notes down to a small integer which was between two recognizable notes. This is what lead to the two notes sounding the same since they'd been rounded to the same value. One helpful thing I've started to do since getting interested in assembly language is turning on listing files in the compiler to see what assembly is generated. This showed that the values calculated where way too small, which I double checked with Compiler Explorer. Listing files are a great tool that have helped me solve several bugs like this. After adjusting the calculation macros, the notes sounded just right, and happy birthday played correctly. 

Soldering everything together was a bit of a challenge. The top PCB with the LEDs has the paper with the happy birthday graphic attached to it, so none of the wiring for the LEDs could go completely through the board since they would have to penetrate the paper too. Instead, all of the wires and resistors are soldered flat on the back. This is a technique that might come in handy someday for something else.


The MSP430, transistors, and buzzer are on a small separate PCB sandwiched between the top PCB and a piece of plexiglass. The battery pack and on/off switch are attached to the plexiglass with hot glue. Although I've never been very impressed with the durability of hot glue, whatever type I got from the craft store this time is absurdly strong and was more than good enough for this project.

Thursday, July 3, 2014

Quiz Buzzer

About a month ago a friend of mine called and asked if I could make a buzzer system for a quiz game for students. Unfortunately, I only had two days until the competition and I did not get it working in time. Hopefully, I can fix it and send it to them to be used at the next competition in a few months.

The plan was that each of ten students would have a button connected to a buzzer by a few meters of wire and the first to press the button after a question was asked would cause a sound and light to go off. The connectors are RCA style and the buttons are from an automotive store.


These lead to an A42 enclosure I was able to sample from Serpac. The enclosure felt really sturdy but the plastic was surprisingly easy to cut and work with with just a knife. I will definitely use them again when I need an enclosure. The circuit itself was fairly simple with just an MSP430 microcontroller, buzzer, two 595 shift registers, and two 165 shift registers. I did not get around to adding the LEDs, although the firmware does use the shift registers to turn on the correct light when someone buzzes in.


To control things I made a separate detachable board with a 16x2 LCD. It will be easier to transport if the cable is detachable and it will also let me add a longer cable if the judges doing the scoring need to sit farther away from the enclosure. Other than the LCD, the board has a 555 to generate negative voltage for the LCD contrast and another 595 shift register to run the LCD. It also has buttons to change the time given to answer and to reset the counter.


For the cable I used Cat-5 again and this may be part of the reason it does not work correctly. For the connector I wanted to make something that would only plug in one way so that people other than me could use it without fear of plugging it in backwards and shorting something. Each side has a pin that can only be connected one way.


A project like this would take me a week at least but I tried to finish in two days because my friends needed it. The night before I stayed up soldering until 7am and when I finally switched it on, the LCD came on as expected and the buzzer sounded when I pressed one of the contestants buttons, but it made a high pitched screeching noise when it was finished buzzing. When I turned it off and tried again the LCD wouldn't display anything and only worked some of the time when I tried it again, so we weren't able to use it in the competition after all.

Why did it fail? In retrospect, I have had problems with this particular LCD in the past. When I was working on my calculator project I noticed that the LCD would often not work when being driven directly by a microcontroller but worked fine when being driven from a shift register like in this project. I'm not sure why it worked before and not now but the fact that it was flaky before might be a clue to my problem. Also, maybe my assumption that it will always work at voltages less than 5v is not correct or maybe supplying negative voltage to the display somehow has consequences. On the other hand, I observed the same faulty behavior with the contrast left unconnected. Another source of problems could be the Cat-5 Ethernet cable I am using for wiring. Each pair is twisted together and running a separate signal through each side of the pair instead of grounding one side might be leading to signal corruption. Finally, the loud screeching of the piezo buzzer is something I will have to figure out as well. Hopefully, when I understand more about this it will help me straighten things out with my clock project also.

Going from nothing to a nearly complete project in only two days was a lot of work, but in the end there was not enough time to troubleshoot problems. When I move into my new apartment next month I will have time to figure everything out and then I can send the working system back to Kyrgyzstan for my friends to use.

Wednesday, April 2, 2014

555 Entropy and Robotic Teddy Bears

Recently I have been keeping busy with a few electronics projects, although it doesn't look like either of them will turn into anything. The first would transmit text wirelessly from a PC to a microcontroller to be displayed on an LCD. For this I planned to hack toy walkie talkies. This is not an ideal way to transfer text but I thought it would be fun to try different ways of making them work to start understanding wireless communication. Unfortunately, in my whole town I could only find one set of walkie talkies and one of them turned out not to work. The data being sent would not be especially sensitive but I still did not want to send it as plain text. This got me wondering about encryption, which I also have no experience with. There are many algorithms but I would need one very easy to compute since the microcontrollers involved would not be very powerful but would have to decrypt and transfer quickly. One solution that intrigued me was using a one time pad. Each microcontroller would have the same list of random numbers that would simply be added or XOR'ed with the data to encrypt and decrypt it. One time pad schemes have the advantage of being truly unbreakable as long as the numbers used are random and never repeated. A bigger advantage for me is that the microcontrollers could simply fetch the numbers from an SD card without having to compute anything. To get truly random numbers, though, I would need a hardware random number generator. These are a common project for hobbysts but not always easy to build correctly. One method uses a transistor in reverse breakdown mode but I decided against this because it requires 15 volts or more. Apparently the transistor also changes with age (perhaps due to being continuously run in a way it was not designed to be) and the characteristics of the numbers generated change too. Another interesting way I found was to measure the output of a 555 timer with a microcontroller ADC as shown here. This made me wonder if I could just measure the length of 555 pulses compared to the oscillator in a microcontroller to use clock drift as an entropy source. I connected a 555 running at 480Hz to the hardware counter of an MSP430 running at 16MHz to measure how many clock cycles each 555 cycle took and dumped 1,000,000 samples out over UART.

Saturday, December 1, 2012

555 Timer Setup

Recently I set up a 555 timer and three AAA batteries to experiment with. The first thing I wanted to do was flash an LED. I wrote a program that will let me figure out the frequency of the flash, size of the capacitor, or the values of the resistors. I decided on a 10uF capacitor and a 100k resistor to give me a blink slow enough to see. This was interesting but then I decided to put a potientiometer in place of R2 so that I could control the rate of the blink. I also put a potentiometer in place of the resistor to the LED so that I could control the brightness. Tuning the potentiometer so that the LED wasn't very dim or very bright was difficult. After doing some reading I saw that it is possible to adjust the on and off times independently of each other if you use a diode with R2. I replaced R1 with a potentiometer so I could control both the length of the flash and the pause between flashes.

Next I connected a piezo speaker to the timer. It ticks every time the current to it is switched on or off. According to the datasheet, the current doesn't have to be turned off but should actually be reversed. I am not sure how to do this with less than four transistors. By using the potentiometers to make the timer pulse quickly, the ticks of the speaker come fast enough to blend into a tone. I tried using smaller capacitors from the TV I took apart with the timer and the tone was much higher. I put another potentiometer in place of the resistor to the speaker to control the volume. One thing I noticed is that the speaker made a growling sound when I tried to turn the volume up too high. It seems that the timer was failing and restarting many times a second because the circuit was drawing more than the 200mA the timer can source. To remedy this I put a resistor in series with the volume potentiometer. This is also a good idea for the other potentiometers so that the timer and LED don't get damaged either.