4. Actuators

In this section we are going to learn about actuators and how to control them.

  1. Piezo Buzzer
  2. Servo Motor

At the beginning of this session you should have collected a kit that is made of:

4.1. Piezo Buzzer

A piezoelectric speaker (sometimes colloquially called a “piezo”) or buzzer is a loudspeaker that uses the piezoelectric effect for generating sound. The initial mechanical motion is created by applying a voltage to a piezoelectric material, and this motion is typically converted into audible sound using diaphragms and resonators.

../_images/piezo-bending.png

When fixed to a metallic diaphragm and excited with an alternating voltage, the diameter of the disc varies by a small amount, this causes dishing of the diaphragm which gives a much louder sound.

4.1.1. Example Circuit

From the kit you are going to need:

  • Buzzer
  • Jumper Wires
  • Arduino
../_images/piezo-buzzer-wiring.png

4.1.2. Code

For the code you can use the Example → 2.Digital → toneMelody. Play around with the sketch and tone() command. You may find it useful for whenever you want to make musical notes. More information on the pitches Arduino library and tone command here. Try also Example → 10.StarterKit_BasicKit → p06 LightTheremin.

4.2. Servo Motor

A servo motor is a rotary actuator or linear actuator that allows for precise control of angular or linear position, velocity and acceleration. It consists of a suitable motor coupled to a sensor for position feedback. It also requires a relatively sophisticated controller, often a dedicated module designed specifically for use with servomotors.

4.2.1. Example Circuit

From the kit you are going to need:

  • Servo Motor
  • Jumper Wires
  • Arduino
../_images/servo-sweep-wiring.png

4.2.2. Code

For this example you are going to use the built-in servo library by Arduino and we are going to use the built-in sketch Example → Servo → Sweep.

../_images/arduino-servo-choice.png
/* Sweep
 by BARRAGAN <http://barraganstudio.com>
 This example code is in the public domain.

 modified 8 Nov 2013
 by Scott Fitzgerald
 http://www.arduino.cc/en/Tutorial/Sweep
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

4.3. Challenge

Important

You must demonstrate your build & code to the tutor team

We challenge you to combine previous sketches (Button, Sweep, tone) to create one that:

  • With the press of a button, sweeps a servo in one direction
  • With the press of a second button, sweeps the same servo in the opposite direction
  • When the servo has swept its maximum travel, the buzzer should sound (beep).