BITE SIZE ARDUINO – PIEZO BUZZER

Today we will have a look at how to connect a piezo buzzer to an Arduino and how to generate different audio signals with it. A piezo buzzer is a audio signalling device, it is the most basic electronic component by which to generate sounds at different frequencies.

A piezo buzzer has 2 connection terminals, one is connected to GND and the other to a PWM digital pin, for this example we will use pin 3.

piezo_bb

We will use the tone() function to generate tones at different frequencies, for an Arduino Uno the frequency rage is between 31 and 65535 Hz. Please note that the possible min and max frequencies differ between different models of Arduino boards. The tone() function takes 3 parameters, firstly the pin to use, then the frequency to use and lastly the duration to generate the tone in milliseconds.

Here is the code used:

#define PIEZO_PIN 3

void setup()
{
}

void loop()
{
     tone(PIEZO_PIN, 31, 1000);
     delay(500); // Pause between Tones 
     tone(PIEZO_PIN, 15000, 1000);
     delay(500); // Pause between Tones 
     tone(PIEZO_PIN, 30000, 1000);
     delay(500); // Pause between Tones 
     tone(PIEZO_PIN, 45000, 1000);
     delay(500); // Pause between Tones 
     tone(PIEZO_PIN, 65535, 1000);
     delay(500); // Pause between Tones 
     noTone(PIEZO_PIN); // Silence Tones
     delay(500);
}
BITE SIZE ARDUINO – PIEZO BUZZER

BITE SIZE ARDUINO – 3 PIN SNAP-ACTION LEVER SWITCH

Today we are looking at how to connect a 3 pin snap-action lever switch to an Arduino board and reading when it is pressed.

switch

The lever switch has 3 pins – the common terminal, the normally off terminal and the normally on terminal. If the switch is not pressed current will flow from the common terminal to the normally on terminal, however if the switch is pressed current will cease flowing from common to normally on and will start flowing from the common to normally off terminals.

For this example we will only utilise 2 of the terminals – the common and the normally off terminal.

The common terminal is connected to the 5V pin on the Arduino board and the normally off terminals’ connection is split:

One leg connecting to the Arduino boards’ ground pin with a 10kOhm resistor in series.
The other leg connecting to a digital pin on the Arduino board, for this example digital pin 2.
lever Switch_bb

Here is the code to determine when the switch is pressed:

#define LEVER_SWITCH_PIN 2
int pressSwitch = 0;
void setup()
{
Serial.begin(9600);
}

void loop()
{
pinMode(LEVER_SWITCH_PIN,INPUT);
pressSwitch = digitalRead(LEVER_SWITCH_PIN);
if(pressSwitch == High)
{
Serial.println(“Switch Pressed!");
delay(1000);
}
}
BITE SIZE ARDUINO – 3 PIN SNAP-ACTION LEVER SWITCH

Bite Size Arduino – Analog vs Digital Pins

I will use the Arduino Bite Size posts to share small bits of information relating to the Arduino platform.

Today we will be examining the 2 main pin types that are present on Arduino boards. For this article I will be referring to the Arduino UNO R3, however all Arduino boards contain these pin types, just note that the quantities of the different pins do vary greatly between the different boards.

There are 2 main groups of pins on any Arduino Board, Analog and Digital pins.

Arduino copy

On this image the Digital pins are highlighted with a red block and the Analog pins with a yellow block.

So what is the difference between the 2 pin types?

Digital pins can read or write 2 possible values HIGH or LOW (1 or 0), whereas Analog pins can read a value between 0 to 1023 and write a value between 0 to 255.

Keeping this in mind it becomes apparent that the main purpose of these pins differ greatly. A Digital PIN can turn an LED on or off, whereas an Analog pin can turn the same LED on to a variety of brightness levels, not just 1. So if you want to simply turn an LED on and off a Digital pin would be the correct pin to use, but for a servo motor signal cable (that controls the movement of a servo motor) an Analog pin would be required as different values (0 to 255) determines how far the servo turns.

This logic also applies when it comes to sensors, for a switch that has 2 states (on and off) a Digital pin would be ideal, however for a IR range sensor it would not work as usually you would like to know a value that represents the distance the sensor is detecting. For this kind of sensor an Analog pin should be used.

So far this all seems pretty straight forward… Now let us consider that some Digital pins can “act” like Analog pins.

You will see by looking at the image on the Arduino above that certain of the Digital pins have ~ next to them. This indicates that the pin is capable of PWM or Pulse Width Modulation. PWM is a method of producing Analog results utilising a Digital means. This is achieved by the Digital pin switching between on and off at a high frequency and thus producing a square wave, where the time in the on state determines the width of the pulses created.

So what does this mean?

If analogWrite(255) is done to a PWM pin it will be in a permanent HIGH (On) state. Whereas analogWrite(0) would place the pin in a permanent LOW (Off) state.

So if analogWrite(127) is done to a PWM pin it will be in a HIGH (On) state roughly 50% of the time and LOW (Off) state for the remaining 50% of the time.  The Arduino PWM frequency is 980Hz, which means that the pin will switch between on and off (HIGH and LOW) approximately every 0.00051 seconds for the 50/50 example above.

Note however that although analogWrite works with PWM pins, analogRead does not. To read a PWM signal the pulseIn method should be used. We will cover the pulseIn method at a later time.

Bite Size Arduino – Analog vs Digital Pins