Showing posts with label Arduino. Show all posts
Showing posts with label Arduino. Show all posts

Monday, July 25, 2011

Arduino Piezo buzzer project with personal method

This is a small project I set up with my Arduino Microcontroller. There are a couple of cool things I learned and realized from this buzzer project. I mainly figured out how to generate a tone with the buzzer in a "manual," custom sort of way, without using the basic tone() function.


Resources:
I learned how piezo buzzers made sound and how I could reproduce it through a couple of resources.

Colin Cunningham's "Colin's Lab" videos include his piezo crystal video, where he grows his own crystals for piezo material and then hits them to produce current. He briefly explains the reverse would make the crystal move.


I was further helped by these two videos, where "cmhiekses" shows how he made his stepper motors play music. Stepper motors are very precise motors that are turned in a mechanical, clock like way. Therefore he was able to move and stop them for very short amounts of time, creating tones.

I had recently learned more about the physics of making sound. Pretty much anything that vibrates the air or pushes on it at higher frequencies makes a sound or a tone, including small or basic mechanical sounds that are repeated quickly.

What I did
I could make "mechanical movements" by giving power to a piezo buzzer, which would alter it at sort of a molecular level. If done repeatedly at a high frequency, these movements would make a sound. So I pulsed power to a piezo buzzer I got from an old electronics kit, using my Arduino. I added a potentiometer to control the pitch.


I thought this was cool because I was creating the sound almost all by myself. I got it to work using the tone() function in the Arduino library, but then turned the pin on and off myself in the code.

In the video above, you can see the blue plastic part (which came with the buzzer) is like a cone that amplifies the sound of the piezo element inside. Without the cone, I found that the sound would be louder even if I just put the element down on the table, letting the flat surface amplify the sound.

Trying it out on an oscilloscope
I wanted to try hooking this up to an oscilloscope just to see what it would look like. I know someone who has one and they let me use it quickly. I was quite surprised at what I saw. Since I'm just turning the pin on and off with simple timing, you would think it would look like a basic square wave, like so:

But in fact it looked like the one below, when the leads were connected in parallel with the piezo speaker.
I'm not exactly sure why it looks this way. I'm guessing it has something to do with the fact that a piezo element will give off current when "moved," as Colin's video above shows. So if the current from the Arduino modifies the element (or it gets modified because it settles down to its original state when the pin is off), it gives off some extra current that show up as the spikes. That would be something like back emf on motors or inductors. I don't know, when I work that out and what it might look like in my head, it doesn't match up. And I don't really know why it would spike so far down in the "off" sections.

The code
The code listens to the potentiometer and translates its value (with a linear equation) to an appropriate tone. It calculates the period of the tone (frequency is how often the repeating event repeats, ex. times per second, where the period is how long one cycle or event is, ex. time it takes for the event to start doing the same thing again), then turns the pin on and off for half a period each.

The conversion
Someone asked me how I figured out the scaling/multiplying factor/the linear equation. Technically you could use some algebra, but I just figured it out in my own unique way. (That's the cool part about understanding the math to make things work.)
First I searched for a note and frequency table online and found some reasonable frequencies I could play that would actually make sounds (2 Hz clearly won't make much of a sound, while I think 20,000Hz is the max for human hearing). I thought between 3500 Hz and 650 Hz was OK. The period is the frequency inverted (so, on a calculator, period = 1 / freq). So the periods would range from 300-6000 microseconds (I made these nice, even numbers since in practice the period is really what I care about).
For the conversion, I had 0-1023 from the analog input and I wanted 300-6000. So, I could multiply the analog input by some scaling factor and make it equal 0-XX, then add 300 to make it 300-6000. Well XX here would be 6000 subtract 300 = 5700 (I'm working backwards here). I divided 5700/1023 to find the scaling factor, which is 5.57184751*. So my equation is (analog input) * 5.57184751 + 300 = new, scaled value(s).

If you're fresh on your middle school algebra, you could treat the IN and OUT values as points on a graph, or points in a function. Then you can use the simple method to find the slope, plug in a point to find the y intercept (which is clearly 300 here anyways), or plug into point-slope form and convert into slope-intercept form.
Or if you're lazy, you could use WolframAlpha, an online computational search engine from Wolfram Research.
http://www.wolframalpha.com/input/?i=equation+of+line+through+%280%2C+300%29%2C+%281023%2C+6000%29
http://www.wolframalpha.com/input/?i=graph+points+%280%2C+300%29%2C+%281023%2C+6000%29


Here is the code: (zoom to see it much better)
Please note that this can be better done with the tone() function (example tutorial), but that my code is a custom method that I used for personal experimentation. I believe some people are copying and using my code for basic buzzer purposes, which is fine, but certainly not the normal method for learning or doing something basic!


/********************
 * Custom Tone 4
 * Ben L
 * 08/14/2011
 * http://noeticbrainwaves.blogspot.com/2011/07/arduino-piezo-buzzer-project.html
 * 
 * Creates a tone on a pezio buzzer in a custom way without the tone() function.
 * This version uses the adequate delayMicroseconds() fuction, instead of the delay()
 * function. It also has an analog input (potentiometer). It uses the more accurate
 * conversion constant, 5.57184751, rather than the previous, mismatched 4.86510264.
 * 
 * This program "pulses" the buzzer's power by rapidly turning voltage on and off.
 * This vibrates the buzzer at frequencies capable of producing sound.
 * The pitch can be set with analog input 0.
 * 
 * The analog input is converted into the period of a reasonable frequency to play
 * on the buzzer, then the period is halved. The buzzer's digital pin is turned
 * both on and off, each with the length of the halved period. The tone will be a 
 * square wave with a 50% duty cycle.
 * 
 * Analog input range is 0-1023,
 * period can be between 300 and 6000 microseconds,
 * frequency can be between 3333Hz and 166Hz
 * e.x. the note A4 has a period of 2272 microseconds and a frequency of 440Hz
 * 
 * Setup: wire a pezio buzzer to digital pin 8 and ground. Wire a potentiometer
 * to analog pin 0 (middle lead goes to pin 0, side leads go to power and ground;
 * if the pezio buzzer has a middle or third lead, just ignore it).
 ********************/


double period;
double wait;


void setup()
{
  pinMode(8, OUTPUT);    //set up DIO pin 8 as an output
}


void loop()
{
  //getting sensor reading and calculating
  period = analogRead(0) * 5.57184751 + 300;    //converting
  wait = period / 2;


  //creating sound by "pulsing" power to the pezio buzzer
  digitalWrite(8, HIGH);     //ON
  delayMicroseconds(wait);   //wait 1/2 period
  digitalWrite(8, LOW);      //OFF
  delayMicroseconds(wait);   //wait 1/2 period
}

Sunday, July 17, 2011

The Awesome Button: a custom USB button How-To

From Makezine.com: http://blog.makezine.com/archive/2011/04/the-awesome-button.html

A nice, simple How-to for repurposing the Staples Easy Button into a USB device. It can be connected to a computer and act like a keyboard or mouse, in this case automatically typing out a synonym for "awesome" when pressed. It uses a TeensyUSB, a spin off (version?) of the Arduino platform connected to the main switch for the button. I really like this hack because it's so simple and opens up a lot of possibilities.

Friday, July 2, 2010

Arduino Digital Display: Count up and Serial Control

My Arduino is currently hooked up to a digital display. This is an electrical component that has LEDs arranged so that you can display a number. To display something, you turn on each LED individually in whatever configuration you like. Right now I have the Arduino hooked up to the digital display with a solderless breadboard. One output pin is connected to each LED of the display. Here is a sketch (Arduino program) to display numbers counting up from 0 to 9 (this repeats, as all Arduino sketches do).

Digital Display: Counting Up
 

This is a sketch that displays numbers that are sent to it from the computer. This is done through USB, but the Arduino and the computer pretend it's an old school serial hookup. You control what's sent through the serial monitor that comes with the Arduino software.

Digital Display: Computer control

I experimented with making my own functions in C with these projects. Instead of manually turning on and off the specific LEDs needed, I made a function that takes in an input number and manipulates the correct LEDs to display that number.

In the future I plan to make a library of the functions I use for the digital display. This allows one to easily include functions into a program without having to manually define them in a big long mess in the beginning.

Thursday, June 17, 2010

Arduino

One of my 'resources' is Arduino. Simply said, this is a small, programmable computer that one can use with electronics projects. They're designed by an Italian open-source organization. Mine looks like this, it's the Arduino Duemilanove:

 
In more detail, it's really an open source system for making such computers, which are called microcontrollers. The Arduino organization has taken this system and made a couple Arduino microcontrolers that you can buy, but the idea is that you can take their plans, parts, and ideas and make your own if you want. This has been very popular in the hobby world and many people have built other versions, helped the Arduino organization with their development, and mostly made other devices to be used as "addons" for the microcontrollers. There are a couple Arduinos that are available to buy: the Duemilanove, Mega, and Nano, and Lilypad. The Duemilanove is the "normal" sized one in terms of input/output ports and memory, the Mega basically has more of everything compared to the Duemilanove, the Nano is designed to be very small and you need to connect to it manually, and the Lilypad is designed for use in fabric or clothing.

Arduino is great because it offers a clear, easy, cheap way of doing projects and experimentation. There is lots of documentation and help and you are free do go at your pace. It's cheap (the Duemilanove costs $30!), although you do need to buy lots of "accessories" like wires and motors that you actually do stuff with. The programming experience is also really great. Arduinos are programmed in the C programming language and the software you can download is very nice and simple. I've worked a little bit with other C programming software and they are very detailed and "heavy duty," which is very hard to learn with. The Arduino software interface is very simplified and there is lots of help out there to teach you how to program.

So I'm very happy with Arduino because it is a great resource for projects and offers a very good learning experience. I'd recommend it over anything else to someone who was interested.