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 – RGB LED

A RGB LED is a LED that can change the colour of the light it produces depending on which of the LEDs’ Connectors have current flowing through them. The LED has 4 connectors, one connector for red, one for green, one for blue and then finally an anode or a cathode, depending if the RGB is a common anode or cathode LED.

So what is the difference between common anode and common cathode?

Well a RGB LED is actually a combination of 3 LEDs, a red LED, a green LED and a blue LED. All LEDs have 2 connectors, an anode and a cathode. So depending how these LEDs are connected together determines if they share an anode or a cathode, thus common anode RGB LED or common cathode RGB LED. The Anode\Cathode leg can be identified as it is the longest leg on the LED. Below are 2 diagrams that illustrates the difference discussed.

Common Cathode:

Common Cathode_schemCommon Anode:

Common Anode_schem

How these 2 different RGB LEDs are connected to a circuit also differs, let us first have a look at a circuit that contains a common cathode RGB LED:

common cathode arduino_bb

Here is the code used with this circuit:

int redPin = 9;
int greenPin = 10;
int bluePin = 11;
 
void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);  
}
 
void loop()
{
  setLEDColour(255, 0, 0);  // red
  delay(2000);
  setLEDColour(0, 255, 0);  // green
  delay(2000);
  setLEDColour(0, 0, 255);  // blue
  delay(2000);
  setLEDColour(255, 255, 0);  // yellow
  delay(2000);  
  setLEDColour(80, 0, 80);  // purple
  delay(2000);
}
 
void setLEDColour(int red, int green, int blue)
{
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);  
}

Now let us have a look at a circuit that contains a common anode RGB LED:

common anode arduino_bb

Code used with this circuit:

int redPin = 11;
int greenPin = 10;
int bluePin = 9;
 
void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);  
}
 
void loop()
{
  setLEDColour(255, 0, 0);  // red
  delay(2000);
  setLEDColour(0, 255, 0);  // green
  delay(2000);
  setLEDColour(0, 0, 255);  // blue
  delay(2000);
  setLEDColour(255, 255, 0);  // yellow
  delay(2000);  
  setLEDColour(80, 0, 80);  // purple
  delay(2000);
}
 
void setLEDColour(int red, int green, int blue)
{
  red = 255 - red;
  green = 255 - green;
  blue = 255 - blue;

  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);  
}

Although the circuits and code differ between the 2 types of RGB LEDs, the end results are exactly the same.

Try changing the values passed into the setLEDColour function to see what different colours can be created.

BITE SIZE ARDUINO – RGB LED

BITE SIZE ARDUINO – SERVO

Today we will have a look at how to connect a servo motor to an Arduino and how to control its movement.

A servo motor has 3 connector wires:
– A red wire for (+) power.
– A black wire for (-) power (GND).
– A orange, yellow or white cable for signal.

servo_bb

The signal wire of the servo must be connected to one of the analog pins on the Arduino, for the purpose of this example we will use A2. The red wire must be connected to the 5V pin on the Arduino and the black wire to the GND pin on the Arduino. As can be seen in the diagram above – a 100uF capacitor is connected between the 2 power terminals, the reason for this is that it prevents a voltage drop occurring in the circuit when the servo starts moving. A voltage drop can occur due to the fact that a servo consumes more power when starting to move then when it is already moving.

Below is the code used to rotate the servo to different positions:

#include "Servo.h" 

#define SERVO_PIN A2

Servo servoMotor;  

void setup()
{
    servoMotor.attach(SERVO_PIN);
}

void loop()
{
     servoMotor.write(0); // Rotate Servo to 0 Degrees
     delay(500); // Delay to allow Servo time to Move
     servoMotor.write(90); // Rotate Servo to 90 Degrees
     delay(500); // Delay to allow Servo time to Move
     servoMotor.write(180); // Rotate Servo to 180 Degrees
     delay(500); // Delay to allow Servo time to Move
}
BITE SIZE ARDUINO – SERVO

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 – Sharp IR Sensor

I have received a few messages asking me to give a bit more detail into the individual bits of the robots I have built so far. Thus I am starting this series of posts in which I will cover a single sensor\actuator integration to an Arduino board per post.

Today we will have a look at how to use a Sharp IR Sensor.

The IR sensor has 3 pins – 2 pins used to power the sensor and a signal pin that is used to communicate its reading.

sharpIR_bb

As shown in the image above, connect the red pin to the 5V pin on the Arduino, the black to the GND pin and the yellow signal pin to any one of the analog pins, for the purpose of this example we will use A4.

Below is the code used to get a reading from the sensor and print it out using the Serial.println:

#define IR_PIN A4
int IRDist = 0; 

void setup()
{
Serial.begin(9600);
}

void loop()
{
pinMode(IR_PIN,INPUT);
IRDist = analogRead(IR_PIN);
Serial.println(IRDist);
delay(1000);
}

I suggest using this code to experiment and see what readings get generated by placing the sensor at different distances from an obstacle.

Bite Size Arduino – Sharp IR Sensor

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

Gates, Build Gates not Bill Gates

The basis of building any logic circuit (even one as complex as a computer) comes down to logic gates. I will be discussing the 2 most basic logic gates today, an AND gate and an OR gate.

Logic gates are physical circuits that implements boolean functions, so to start let us look at the boolean AND and OR functions.

For all the examples below let us assume that we have 2 inputs: A and B, and that A and B both have 2 possible states: on or off, 1 or 0 in binary terms.

AND Function

An AND function requires both A and B to be in an “on” state to give a positive “on” result. (Just note the NAND function will give a positive “on” output when A and B are NOT both “on”. We will look at the NAND function and gate in detail at a later time).

Below is the state table for the AND function with inputs A and B as well as the resulting output: 

A B Output
0 (off) 0 (off) 0 (off)
1 (on) 0 (off) 0 (off)
0 (off) 1 (on) 0 (off)
1 (on) 1 (on) 1 (on)

OR Function

An OR function requires either A or B  (or both) to be in an “on” state to give a positive “on” result. (Just note the NOR function requires neither A or B to be in an “on” state to give a positive “on” result. Additionally the XOR function will give a positive “on” output only when A or B are “on” but NOT when both are “on”. We will also look at the NOR and XOR functions and gates in detail at a later time).

Below is the state table for the OR function with inputs A and B as well as the resulting output: 

A B Output
0 (off) 0 (off) 0 (off)
1 (on) 0 (off) 1 (on)
0 (off) 1 (on) 1 (on)
1 (on) 1 (on) 1 (on)

Now let us examine the gate circuits. (I have constructed both gates on a Adafruit Perma-Proto board shown in the picture below).

Gates

AND Gate:

Parts required:

  • 3  resistors (10k Ohm will do)
  • 2 push buttons (input A and B)
  • 2 BJT NPN transistors
  • 1 LED (output)

And Gate_bb

AND GATE

AND Gate Schematic

So by pushing the buttons in accordance to the AND state table above the outputs can be recreated. Because the 2 transistors are placed in series the circuit can only be completed when both button A and B are pressed, and thus the AND function is implemented.

OR Gate:

Parts required:

  • 3  resistors (1 x 10k Ohm and 2 x 660 Ohm resistors will do)
  • 2 push buttons (input A and B)
  • 2 BJT NPN transistors
  • 1 LED (output)

Or Gate_bb

OR Gate

OR Gate Schematic

So by pushing the buttons in accordance to the OR state table the corresponding outputs can be recreated. Because the 2 transistors are placed in parallel the circuit can be completed by pressing either the A or B button (or both). The circuit thus represents the OR function.

Just note the selection of resistor sizes are not cast in concrete, just pick a resistance high enough so your transistor does not get fried based on your power supply size (in my case a 9 Volt battery). I simply chose the resistors based on what I had available at the time.

Additionally if the role of the transistors in the circuit does not make sense to you please look at my earlier post (TRANSISTOR CRASH COURSE) that explains the functioning of transistors and their roles in circuits.

Gates, Build Gates not Bill Gates