Logo ilsitodelfaidate.it

10.1 What Are Actuators? A Beginner’s Guide for Arduino

In the previous chapters, we’ve learned how to use Arduino to pick up information from the world around it through buttons and sensors. We’ve seen, for example, how to measure light levels with a photoresistor (also known as a photocell or light-dependent resistor, LDR), measure distance with an ultrasonic sensor and detect movement with a PIR motion sensor.

But an electronic system doesn’t just collect information: it often has to react to what’s going on, too. It might switch on a light, make a sound, move an object or trigger a mechanism. To do all this, we use components called actuators.

What Is an Actuator?

An actuator is a device that turns a command, together with the energy it receives, into a physical action. Depending on the type of component, that action might produce light, sound or movement, or switch on another device.

There are plenty of different types of actuators. Here are the ones we can use in our Arduino projects:

  • LEDs: give off light when they’re powered correctly.
  • Buzzers: produce sounds and beeps (audible signals).
  • Servo motors: let us control the angular position of a small mechanism.
  • DC motors (direct current motors): turn electrical energy into continuous rotation.
  • Relays: let us open or close (switch) the contacts of another circuit electrically.

These components all do different jobs, but they have one thing in common: they receive a command and let the system take action.

Sensors and Actuators: Two Roles That Work Together

To get a better feel for what actuators do, it helps to compare them with the sensors we looked at in the previous chapter.

A sensor gathers information about its surroundings and turns it into a signal that Arduino can read. An actuator, on the other hand, carries out an action in response to a command.

We can sum up how a simple system works like this:

Environment → Sensor → Arduino → Actuator → Action

Let’s imagine, for example, that we want a light to come on automatically when a room gets dark. A light sensor detects how much light there is in the room and sends a signal to Arduino. The program compares the value it receives with a preset threshold. If the light level is too low, Arduino switches the LED on.

In this case, the photoresistor is the sensor, Arduino processes the information and the LED plays the part of the actuator.

The same principle works in a more complex system. An ultrasonic sensor can detect an object, Arduino can work out how far away it is, and a servo motor can point a device in the direction we want.

How Does Arduino Control an Actuator?

Arduino controls actuators through its output pins and, when necessary, through dedicated driver circuits or modules. The method depends on the type of component you’re using.

An LED, for example, can be controlled with a digital output: Arduino sets the pin to HIGH to switch it on and to LOW to switch it off, using a suitable connection and a current-limiting resistor.

A buzzer can be driven to produce an audible signal, while a servo motor needs a specific control signal that tells it which position to move to.

It’s important to remember that the control signal doesn’t necessarily supply all the power the actuator needs. An Arduino pin can drive some low-current components directly, but it isn’t designed to power just any motor or device. Actuators that draw more current may need a driver circuit or a separate power supply.

Before connecting any component, you should therefore check its electrical specifications and how it’s meant to be controlled.

Diagram showing the flow from environment to sensor, Arduino, actuator and action. Sensors on the left (photoresistor, HC-SR04 ultrasonic sensor, PIR motion sensor), an Arduino UNO R3 board in the middle, and actuators on the right (LED, buzzer, SG90 servo motor, DC motor, relay). At the bottom, an example of an LED switching on in a dark room.

What Are We Going to Build in the Next Sections?

We’ll start with a simple actuator, the buzzer, and learn how to make sounds with Arduino. Then we’ll move on to the SG90 servo motor, a small motor that’s very popular in educational robotics projects.

We’ll learn how to connect it, control its position and drive it from a program. Finally, we’ll use a potentiometer to change the servo’s position in real time.

These activities will let us combine what we’ve learned about sensors, variables and conditions with the control of a moving device, setting us up for the course’s final robotics project.

10.2 How to Use a Buzzer with Arduino: Making Sounds and Beeps

Now that we know what actuators are, let’s start with a simple component that’s handy in lots of projects: the buzzer, a device that produces audible signals.

We can use it to signal an event, show that a sensor has detected something or alert the user when a certain condition is met. For example, an obstacle detection system can sound a beep when an object gets too close.

What Is a Buzzer?

A buzzer is an electronic component that turns electrical energy into vibrations that create a sound. There are several types of buzzer, but for Arduino projects it’s worth knowing about two main categories in particular: active buzzers and passive buzzers.

An active buzzer contains an electronic circuit that generates, all by itself, the signal needed to produce a sound. When it’s powered correctly, it usually gives out a tone at a fixed frequency. This means Arduino can control it mainly by switching it on and off.

A passive buzzer, on the other hand, can’t generate the tone by itself: it needs to receive an oscillating electrical signal. By changing the frequency of that signal, Arduino can produce higher-pitched or lower-pitched sounds and even play simple tunes.

The two types can look very similar. Before you use one, it’s therefore important to check the manufacturer’s information or the module’s documentation.

Connecting the Buzzer to Arduino

For this first experiment we’ll use a passive buzzer, so that we can learn to control the frequency of the sound as well.

What you’ll need:

  • An Arduino UNO R3 board with a USB cable to connect it.
  • A passive buzzer that’s compatible with the circuit.
  • Two jumper wires.

For a two-terminal passive buzzer that’s suitable for direct connection, make these connections:

Buzzer Arduino UNO R3
Positive terminal (+) Digital pin 8
Negative terminal (−) GND

 

The positive terminal is often marked with a + symbol or has a longer leg, but not all components are marked in the same way. So check the markings on the model you’re using.

Fritzing wiring diagram showing a passive buzzer connected to an Arduino UNO R3 using a breadboard. The buzzer’s positive terminal goes to digital pin 8 with a green jumper wire, and the negative terminal goes to GND with a black jumper wire.

Some buzzers need more current than an Arduino pin can safely supply. If the component isn’t suitable for direct driving, you’ll need to use an appropriate driver circuit. For our experiment, choose a buzzer that’s compatible with the board’s electrical specifications.

Our First Program: Making a Sound

Now let’s draw a flowchart for a program that plays a tone for one second, then pauses for one second, repeating this cycle forever.

Flowchart of an Arduino buzzer program: set pin 8 as an output, turn on a 1 kHz tone, wait one second, turn the tone off, wait one second, then repeat the loop from the start.

Writing the Program in the Arduino IDE

Now we can write the code (or sketch, as Arduino programs are often called), using the tone() function to generate the sound and noTone() to stop it.

const int buzzerPin = 8;
void setup() {
}
void loop() {
  tone(buzzerPin, 1000);
  delay(1000);
  noTone(buzzerPin);
  delay(1000);
}

Upload the program to Arduino. You should hear a tone, followed by a pause, repeating over and over.

The tone() function generates a square wave at the frequency you specify. In this example, the value 1000 corresponds to a frequency of 1000 hertz (Hz), that is, 1000 oscillations per second.

The noTone() function stops the tone from being generated on the pin you specify. The delay(1000) instructions add pauses of one second, just like we saw in our earlier experiments.

The setup() function is empty because, for this simple example, we don’t need to explicitly configure the pin with pinMode(): the tone() function handles the signal on the specified pin directly.

Changing the Pitch (Frequency) of the Sound

We can change the second parameter of tone() to get a different tone.

For example:

tone(buzzerPin, 500);

produces a 500 Hz tone, while:

tone(buzzerPin, 2000);

produces a 2000 Hz tone.

In general, raising the frequency makes the sound higher-pitched, and lowering it makes the sound lower. The actual result also depends on the characteristics of the buzzer you’re using.

Making a Simple Alarm Signal

Now let’s try creating a signal that alternates between two frequencies, simulating a little alarm.

const int buzzerPin = 8;
void setup() {
}
void loop() {
  tone(buzzerPin, 1000);
  delay(300);
  tone(buzzerPin, 1500);
  delay(300);
  noTone(buzzerPin);
  delay(500);
}

In this program, the buzzer first plays a 1000 Hz tone and then a 1500 Hz one. Finally, the sound stops for half a second and the sequence starts again.

We don’t need to call noTone() between the two tones: the second call to tone() simply changes the frequency of the signal on the same pin.

Exercises and Variations

Now try changing the program to experiment with sound.

  1. Change the frequency from 1000 Hz to 700 Hz and compare the two sounds.
  2. Shorten the length of the tones from 300 to 100 milliseconds.
  3. Create a signal with three different frequencies.
  4. Change the pauses to get a slower or faster alarm.

For an extra challenge, you can go back to the circuit with the HC-SR04 ultrasonic sensor and change the program so that the buzzer sounds when an object is less than 20 cm (about 8 inches) away. That way, you’ll combine a sensor, a condition and an actuator in a single project.

In the next section we’ll move from sound to motion and find out how to control a servo motor, one of the most useful components for our robotics projects.

10.3 What Is a Servo Motor? Controlling Position with Arduino

Now that we’ve learned how to make sounds with Arduino, let’s move on to an actuator that lets us control movement: the servo motor (or simply “servo”).

Servo motors are very popular in robotics because they can move a mechanical part to exactly the position you want. You’ll find them, for example, in robotic arms, camera pan-and-tilt systems, radio-controlled (RC) models and any mechanism that needs to rotate in a controlled way.

In this course we’ll use a small servo called the SG90, which is perfect for lots of learning experiments. Later on, we’ll use it to point the ultrasonic sensor in our final project.

What Is a Servo Motor?

A servo motor is a device that lets you control the position of a moving part. A common model like the SG90 combines a tiny electric motor, a set of gears, a control circuit and an internal system that senses the position of the output shaft.

When it receives a command, the servo compares its current position with the requested one and drives the motor to get closer to the position you want.

This kind of control system is called feedback (or closed-loop control): the device uses information about its own position to correct its movement.

Unlike a simple DC motor, which normally spins continuously whenever it’s powered, a servo of this type is designed to reach and hold a specific angular position.

What Does Controlling an Angle Mean?

The movement of a servo motor is normally described by an angle, measured in degrees (°).

For example, we can ask the servo to move to 0°, 90° or 180°. These values are the angular positions we request with the command, relative to the model’s nominal range.

For the common SG90, the command range is often given as roughly 0°–180°. However, the angle it can actually reach may vary slightly from one unit to another.

It’s important not to confuse angular position with speed: asking the servo to go to 90° means telling it a position, not directly specifying how fast it should move.

Blue SG90 micro servo motor (9 g) with its three-wire cable and connector, lying on a workbench next to three white plastic servo horns and mounting screws.

How Do You Control a Servo Motor?

A standard servo like the SG90 normally has three wires:

  • Positive power (VCC): often red, connected to the voltage the model is designed for.
  • Ground (GND): often brown or black.
  • Control signal: often orange, yellow or white.

These colours are common, but they can vary from one manufacturer to another. Before making any connections, always check the information supplied with your component.

The signal wire receives a command from Arduino that tells the servo which position to move to. In standard servos, this command is generally a signal made up of repeated pulses (a PWM-type signal), where the length of each pulse tells the servo the position you’re asking for.

So this isn’t an ordinary digital command that just holds the pin HIGH or LOW to switch the motor on or off. We need to generate a control signal that’s suited to the servo motor.

In the next section we’ll see how to physically connect the SG90 to an Arduino UNO R3 and how to use the Servo library to control its movement.

Watch Out for the Power Supply

Even though a servo is small, the current it draws can rise sharply when it starts moving, changes direction or meets mechanical resistance.

For this reason, you shouldn’t assume that just any servo can be powered straight from Arduino’s 5V pin under all conditions. The right power supply depends on the characteristics of the motor, the load and the rest of the circuit.

When you use a separate power supply for the servo, its ground must be connected to Arduino’s ground so that the control signal has a common reference. You also need to respect the voltage the servo motor is rated for.

In this course we’ll start with a simple connection that’s suited to the experiment, keeping these precautions in mind right from the start.

10.4 How to Control a Servo Motor with Arduino: Your First Servo Program

Now that we know how a servo motor works, we can connect it to Arduino and write our first program to control its position.

What You’ll Need

For this activity you’ll need:

  • An Arduino UNO R3 board.
  • An SG90 servo motor.
  • Jumper wires.
  • A breadboard, if it helps you organise the connections.
  • A USB cable to connect Arduino to your computer.
  • A power supply suited to the servo motor, if needed.

Connecting the Servo Motor

The SG90 servo motor generally has three wires, each with its own job:

  • Red: positive power, normally 5 V.
  • Brown or black: GND, that is, ground.
  • Orange, yellow or white: control signal.

The colours can vary depending on the model, so it’s always a good idea to check the manufacturer’s information.

For our experiment, we’ll connect the signal wire to Arduino’s digital pin 9. The GND wire must be connected to the board’s ground, while the positive wire goes to a power supply that’s suitable for the servo motor.

For a simple test, with the servo running without a load, the board’s 5 V supply can be enough in some setups. However, a servo can draw a relatively high current, especially at start-up or when it has to move a mechanical part. For more reliable operation, it’s better to use a regulated power supply suited to the servo whenever it’s needed. In that case, the power supply’s GND must also be connected to Arduino’s GND, so that both share the same electrical reference.

Warning: never connect the servo motor to a digital pin to power it. The digital pin is there to send the control signal, not to supply all the power the motor needs.

 

Fritzing wiring diagram showing an SG90 servo motor connected to an Arduino UNO R3 using a breadboard. The yellow signal wire goes to digital pin 9, the red wire to the 5V power rail, and the black wire to the GND rail.

Drawing the Flowchart

Our first program will simply move the servo to the 0° position, then move it to 90° and finally move it to 180°, before starting the cycle again.

After each move there will be a one-second pause before the servo goes on to the next position.

The flowchart could look like this:

Flowchart of an Arduino servo program: attach the servo to pin 9, move it to 0 degrees, wait one second, move it to 90 degrees, wait one second, move it to 180 degrees, wait one second, then repeat the sequence from 0 degrees.

Preparing the Program

To control the servo motor we’ll use the Servo library, which normally comes with the Arduino IDE. A library is a collection of ready-made functions that makes it easier to work with a component.

Let’s write this program (or sketch):

#include <Servo.h>
Servo myServo;
void setup() {
  myServo.attach(9);
}

void loop() {
  myServo.write(0);
  delay(1000);
  myServo.write(90);
  delay(1000);
  myServo.write(180);
  delay(1000);
}

Let’s upload the program to the Arduino board and watch the servo move. After start-up, the servo should move to a position close to 0°, wait one second, move towards 90°, wait again and finally reach a position close to 180°. Then the cycle starts over.

The actual positions may vary slightly depending on the model and its mechanical limits.

Understanding the Code

The first line:

#include <Servo.h>

tells the compiler that we want to use the Servo library, which contains the instructions needed to control the servo motor.

The line:

Servo myServo;

creates an object called myServo, which we’ll use to send commands to the motor. We chose the name ourselves and it could be anything else, as long as we use it consistently throughout the program.

In the setup() block we find:

myServo.attach(9);

This instruction links the servo motor to Arduino’s pin 9, where we connected the signal wire. This way, the library knows which pin to use to generate the control signal.

In the loop() block we use:

myServo.write(0);

The write() command tells the servo motor the position we want, normally given in degrees. With write(0) we ask for a position close to 0°, with write(90) a central position and with write(180) a position close to 180°.

Finally, the delay(1000) instructions add a one-second pause between one command and the next. This lets us see each movement more clearly.

Making the Servo Sweep Back and Forth

We can change the program so that the servo moves gradually from one side to the other. That way the movement will be smoother and more like a scan, or sweep.

As usual, let’s start by drawing the flowchart:

 

Flowchart of an Arduino servo sweep program: attach the servo to pin 9, then a first loop moves the angle from 0 to 180 degrees, setting the servo position and waiting 15 milliseconds at each step. A second loop brings the angle back from 180 to 0, and the whole sequence repeats.

Let’s replace the contents of the previous program with the following code:

#include <Servo.h>
Servo myServo;
void setup() {
  myServo.attach(9);
}

void loop() {
  for (int angle = 0; angle <= 180; angle++) {
    myServo.write(angle);
    delay(15);
  }
  for (int angle = 180; angle >= 0; angle--) {
    myServo.write(angle);
    delay(15);
  }
}

The first for loop starts at 0 and increases the angle variable by one degree at a time, until it reaches 180°. The second loop starts at 180° and decreases the angle until it’s back at 0°.

The delay(15) instruction gives the servo motor a short interval to start moving towards each position. The real movement depends on the servo’s speed and the mechanical load: the motor doesn’t necessarily move exactly one degree every 15 milliseconds.

Test It and See

After uploading the program, let’s watch how the servo motor behaves and check that it:

  • moves without any odd interruptions;
  • reaches the requested positions without being obstructed;
  • doesn’t vibrate or make unusual noises;
  • doesn’t make Arduino restart (reset) while the servo is moving.

If the servo vibrates, stalls, gets too hot or makes the board restart, let’s stop the test and check the power supply and the connections. Let’s not force the mechanism by hand, and let’s not keep going if the motor seems to be hitting a mechanical limit.

With this activity we’ve learned how to control a servo motor with a program and how to use a loop to generate a sequence of positions. In the next section we’ll add a potentiometer, so that we can control the servo’s angle directly by turning a knob.

10.5 Controlling a Servo Motor with a Potentiometer and Arduino

In the previous section we learned how to control a servo motor using a program that sent it a sequence of preset positions. Now we’ll build a more interactive system: we’ll use a potentiometer to adjust the servo’s angle directly.

A potentiometer (often called a “pot”) is an electronic component with a rotating knob or shaft. By turning it, we can change its resistance and, with the right connection, get a variable voltage that Arduino can read through an analogue (US: analog) input.

In this project, Arduino will read the position of the potentiometer and convert it into the angle we want for the servo motor. That way, we can control the motor’s movement just by turning the knob, without having to change the program every time.

What You’ll Need

To build the circuit you’ll need:

  • An Arduino UNO R3 board.
  • An SG90 servo motor.
  • A potentiometer, preferably 10 kΩ.
  • A breadboard.
  • Jumper wires.
  • A USB cable to connect Arduino to your computer.
  • A suitable power supply for the servo motor, if needed.

Connecting the Potentiometer

A standard potentiometer has three terminals: the two outer terminals are connected to the ends of the internal resistive track, while the middle one is connected to the wiper, which changes position as we turn the knob.

To connect it to an Arduino UNO R3, make these connections:

  • One outer terminal of the potentiometer to Arduino’s 5V pin.
  • The other outer terminal to the GND pin.
  • The middle terminal to analog pin A0.

With this connection, as we turn the knob, the voltage on pin A0 will vary from roughly 0 V to 5 V. The value Arduino reads will normally be between 0 and 1023.

If the knob turns in the opposite direction to the one you want, you can simply swap the two outer terminals.

Connecting the Servo Motor

We’ll keep the same servo connection we used in the previous section:

  • Signal wire to digital pin 9.
  • GND wire to Arduino’s GND.
  • Positive wire to a suitable power supply, normally 5 V for the SG90 model, checking the component’s specifications.

If we use a separate power supply for the servo motor, we need to connect the power supply’s GND to Arduino’s GND. That way, the control signal has a common electrical reference.

Let’s not power the servo motor from a digital pin. And if the movement makes the board restart or behave erratically, let’s check the power supply and consider using a 5 V source that’s better suited to the motor.

Fritzing wiring diagram showing an Arduino UNO R3 with a breadboard, an SG90 servo motor and a potentiometer. The servo’s yellow signal wire goes to digital pin 9, the potentiometer’s middle pin (green wire) goes to analog input A0, and the red and black wires bring 5V and GND to both components through the power rails.

Drawing the Flowchart

As we’ve already mentioned, in this program Arduino will read the position of the potentiometer and convert it into the angle we want for the servo motor. With the potentiometer turned all the way to the left, the servo will be at roughly 0°. As we turn it to the right, the servo will move gradually until, with the potentiometer turned all the way to the right, it reaches a position equivalent to about 180°.

Flowchart of an Arduino program that controls a servo with a potentiometer: attach the servo to pin 9, read the potentiometer value on pin A0, convert it to an angle between 0° and 180°, send the position command to the servo and wait 15 milliseconds, then repeat from the potentiometer reading.

Writing the Program

Now we can write the program that reads the potentiometer and controls the servo motor.

/*
Exercise No. 10.5
Purpose: Moves a Servo from 0° to 180° by turning a potentiometer
Board: Arduino UNO R3
Author: Luciano (Capitan Farloc)
Version: 1.0
Version: 1.1 – Added Serial Monitor (commented out) for debugging
Connections:
  - Potentiometer 10 kOhm: Analog pin A0
  - Servo SG90: Digital pin 9
*/
#include <Servo.h>
Servo myServo;
const int servoPin = 9; // Servo control pin
const int potPin = A0; // Potentiometer reading pin
int potValue;
int angle;
void setup() {
  myServo.attach(servoPin); // Initialize the Servo
  Serial.begin(9600); // Initialize the serial console
}

void loop() {
  potValue = analogRead(potPin);
  angle = map(potValue, 0, 1023, 0, 180);
  myServo.write(angle);
  delay(15);
  // Write the potentiometer value and the resulting angle to the Serial Monitor
  // Serial.print(“Pot: “);
  // Serial.println(potValue);
  // Serial.print(“Angle: “);
  // Serial.println(angle);
  // delay(300);
}

Let’s upload the program to Arduino and slowly turn the potentiometer’s knob. The servo motor should change position, following the movement of the knob.

Understanding the Code

At the start, we include the Servo library and create the myServo object, which we’ll use to control the motor. Then we define the pins we’re using and the variables we’ll need:

const int potPin = A0;

const int servoPin = 9;

int potValue;

int angle;

The potPin constant tells us which analog input to read the potentiometer’s value from. The servoPin constant, on the other hand, is the pin that sends the control signal to the servo motor.

The potValue variable stores the reading taken by Arduino, while angle holds the position we want to command the servo to move to.

In the setup() block we find:

myServo.attach(servoPin);

This instruction links the servo motor to pin 9.

In the loop() block, we continuously read the potentiometer:

potValue = analogRead(potPin);

The analogRead() function returns a value between 0 and 1023 on the Arduino UNO R3 board. The value changes according to the voltage on pin A0.

However, we need to convert this range into another one: the range of angles we want to send to the servo motor. To do this, we use the map() function:

angle = map(potValue, 0, 1023, 0, 180);

The map() function converts a number from one range to another. In this case, a value close to 0 corresponds to an angle close to 0°, a value close to 512 corresponds to roughly 90° and a value close to 1023 corresponds to an angle close to 180°.

The conversion is proportional, but it uses whole numbers (integers): for this reason, some in-between values are rounded to the nearest whole number the calculation allows.

Finally, we send the calculated value to the servo motor:

myServo.write(angle);

The motor now gets a new position to move to. The short 15-millisecond pause lets the program repeat the reading at regular intervals, without introducing long waits.

Let’s Try It Out and Experiment

Once we’ve checked that the circuit works, we can try a few things:

  1. Slowly turn the knob from one end to the other and watch how the servo moves.
  2. Stop the knob at a middle position and check that the servo motor holds a matching position.
  3. Try swapping the two outer terminals of the potentiometer and see how the direction of control changes.
  4. Change the line that uses map() to limit the range of movement, for example from 20° to 160°:

angle = map(potValue, 0, 1023, 20, 160);

This way, the potentiometer will still control the servo, but the position we command will stay within the range of 20° to 160°. This limit can be useful when the mechanism connected to the servo shouldn’t reach the extreme positions.

Remember that the angles in the program are the positions we ask for, not a guarantee that every servo motor can reach exactly the same limits. If the motor vibrates, stalls or hits a mechanical obstacle, we need to reduce the range and check how everything is mounted.

With this exercise we’ve connected an analog input to an actuator: the potentiometer provides a value, Arduino processes it and the servo motor carries out the command. It’s a simple but important example of how an electronic system can turn a user’s action into a controlled movement.

10.6 Powering Arduino Actuators Safely: Power Supply Tips and Precautions

We’ve seen that Arduino can control several different actuators, such as LEDs, buzzers and servo motors. However, not all of these components need the same amount of power, and not all of them can be powered directly from the board.

To build reliable circuits, it’s important to understand how to power your components properly and what precautions to take to avoid malfunctions or damage.

Powering Arduino and Its Components

An Arduino UNO R3 can be powered over USB or from an external power supply that’s compatible with the board’s specifications. When we connect a component to Arduino’s pins, we need to tell apart two functions:

  • Power: supplies the energy the component needs to work.
  • Control signal: tells the component which action to carry out.

With the SG90 servo motor, for example, the positive and GND wires are for power, while the third wire receives the signal that tells it the position we want.

The fact that Arduino can send a command to a component doesn’t necessarily mean it can also supply all the current needed to power it.

Why a Servo Motor May Need a Separate Power Supply

A servo motor contains a small electric motor and an electronic control system. When it starts up, changes position or has to move an object, it can need more current than it draws when it’s standing still.

If the current demanded is too much for the power supply you have, several problems can occur:

  • Arduino may suddenly restart (reset).
  • The servo motor may vibrate or move erratically.
  • The circuit may become unstable.
  • The supply voltage may drop temporarily (a “brownout”).

For this reason, in simple tests a small servo motor can work from the board’s 5 V supply, but this shouldn’t be considered a solution that suits every situation. If the motor is under load or the circuit is having problems, you may need to use a separate regulated power supply that can provide the current required.

When we use a separate power supply, we need to connect the power supply’s GND to Arduino’s GND, so that the control signal has a common reference. The power supply’s voltage must be compatible with the servo motor.

Precautions with Other Actuators

The other components need a bit of care, too.

LEDs: they must be connected with the correct polarity and, normally, with a series resistor to limit the current. Never connect them directly to a digital output without proper current limiting.

Buzzers: before connecting one, check whether it’s active or passive and read the component’s specifications. Not all buzzers have the same electrical characteristics, and not all of them can be driven directly from an Arduino pin.

DC (direct current) electric motors: they can draw high currents, especially at start-up or when the shaft is blocked (stalled). To control them, we normally use dedicated driver circuits, such as transistors or motor drivers. They must never be connected directly to Arduino’s digital pins to be powered.

Relays: they let us switch an electrical circuit using a control signal, but the circuit being switched may carry dangerous voltages. In particular, working with mains voltage (US: line voltage, or household AC) requires suitable components, proper insulation and specific skills. In this course we’ll limit ourselves to understanding the principle of operation, without making any connections to the mains.

A Few Rules to Remember

Before powering up a circuit with an actuator, it’s good practice to follow these guidelines:

  1. Check the supply voltage the component requires.
  2. Check the current the component can draw and the current the power source can supply.
  3. Correctly identify the power and signal terminals.
  4. Don’t use a digital pin as a power source for motors or other loads that need more current than the pin is allowed to give.
  5. Switch off or disconnect the power before changing any connections.
  6. If you notice restarts, unusual vibrations, overheating or strange smells, stop the test straight away and check the circuit.

A well-designed circuit doesn’t just have to work: it has to work reliably, and within the electrical limits of its components.

Photo of a working breadboard circuit: an Arduino UNO board in a clear case, connected to a computer with a USB cable, an SG90 servo motor with its plastic horn and a potentiometer with a knob, all linked by coloured jumper wires.

In the next section we’ll put what we’ve learned into practice with some exercises and applications.

10.7 Arduino Actuator Exercises: Practice with the Buzzer, Servo Motor and Potentiometer

In this section we’ll put what we’ve learned about actuators into practice, using LEDs, a buzzer and a servo motor. The exercises start with simple changes to the programs we’ve already written and move on to activities that need you to combine several components.

The aim is to learn how to turn a command written in the program into a real-world action, and to see how Arduino can control lights, sounds and movement.

Exercise 1: Changing the Buzzer’s Sound

Let’s go back to the program that uses tone() and noTone().

Let’s change it to meet these conditions:

  • The buzzer plays a 500 Hz tone for half a second.
  • Then it plays a 1500 Hz tone for half a second.
  • Finally, it stays silent for one second, then repeats the sequence.

Questions:

  1. Which parameter of the tone() function sets the pitch of the sound?
  2. What happens if we increase the frequency?
  3. What is the noTone() function for?

Exercise 2: Controlling the Servo Motor with Preset Positions

Let’s write a program that makes the servo motor move, one after the other, to these positions:

  • 30°.
  • 90°.
  • 150°.

The servo should wait one second at each position before moving on to the next.

Once we’ve checked that it works, let’s change the angles and see how the movement changes. Let’s not force the servo motor if it hits a mechanical limit, and let’s remember that the actual angles can vary depending on the model.

Questions:

  1. What is myServo.attach(9) for?
  2. What does the value passed to myServo.write() mean?
  3. What’s the difference between the position of a servo motor and the speed of a continuous-rotation motor?

Exercise 3: Controlling the Servo Motor with the Potentiometer

Let’s go back to the circuit with the potentiometer connected to A0 and the servo motor connected to pin 9.

Let’s change the program so that the servo motor can only move between 20° and 160°. We can use the map() function to convert the value read from the potentiometer into the range we want:

angle = map(potValue, 0, 1023, 20, 160);

Let’s slowly turn the knob and check that the servo follows the movement without reaching the extreme positions.

Questions:

  1. Why do we use analogRead() to read the potentiometer?
  2. What’s the range of values returned by analogRead() on an Arduino UNO R3?
  3. Why do we use map() before sending the command to the servo motor?

Exercise 4: Building a Warning Beeper

Let’s build a small system that uses the HC-SR04 ultrasonic sensor, which we studied in the previous chapter, to control a buzzer.

The behaviour we need is as follows:

  • If no valid echo is detected, the system must not treat the result as a real distance.
  • If an object is more than 30 cm (about 12 inches) away, the buzzer stays off.
  • If an object is 30 cm (about 12 inches) away or closer, the buzzer sounds a warning tone.

To do the exercise, let’s start from the program that reads the HC-SR04 sensor and add the instructions needed to control the buzzer. Let’s remember to check that the measurement is valid before comparing it with the distance threshold.

Goal: to combine a sensor and an actuator, making an action depend on a measurement

Infographic showing the four exercises of the chapter: 1) a buzzer playing 500 Hz and 1500 Hz tones for half a second each, then one second of silence; 2) an SG90 servo motor moving to 30°, 90° and 150° with a one-second pause; 3) a servo controlled by a potentiometer using the map function from 20° to 160°; 4) an ultrasonic sensor alarm that turns the buzzer on at 30 cm or less.

Coming up in the next chapter...

So far, we’ve learned to work with one component at a time: reading a sensor, showing a message on a display, making a buzzer sound, moving a servo motor. But the really exciting projects begin when all these pieces stop working on their own and start working together.

In the next chapter, Chapter 11 – Arduino Project with HC-SR04 Ultrasonic Sensor, LCD Display and Buzzer: Making Components Work Together, we’ll take exactly that step. We’ll put together an ultrasonic sensor, a display, a buzzer and a servo motor to build a small device that can notice what’s going on around it and react.

We’ll find out how to think things through before we even write the code, how to organise the program neatly and how to add one feature at a time without getting lost along the way.

By the end, we’ll have an obstacle detector that “looks” in different directions and warns us when something gets too close, plus a few challenges to put our imagination to the test.

This is where our individual components become a real project, and our first real taste of what it means to build a robot.

If you’re curious to see where this journey will take us, have a look at the page Beginner's Guide to Robotics with Arduino: there you’ll find the full contents list, from the first chapter to the final project, and you can choose whether to carry on in order or go back and review a topic.

If you’ve found this course useful and would like to explore other robotics or hobby electronics projects, you can keep reading by visiting the other content in the Electronics and Robotics section of the site, where you’ll find plenty more practical guides and step-by-step explanations.

I’d like to point out that some of the images in the course were generated by artificial intelligence (AI), and I apologise for any inaccuracies they may contain. The course itself was also written with the help of AI, but the structure is the result of my own experience: I’m a retired programmer and, over the course of my career, I’ve taught several courses. So I chose the topics, organised the whole structure of the course and reviewed the text myself.

I care a great deal about the quality and originality of the content I publish. That’s why I’d prefer it not to be copied or republished elsewhere. If you think this guide could be useful to other people, though, I’ll be really happy if you share the link to the page: it’s the right way to spread the information and to support the work behind every article.

Thank you

Luciano (Capitan Farloc)

Warning!

The advice and instructions provided on this site come from hobbyists rather than professional technicians, meaning there is no guarantee that they are accurate or represent the best solution.
Additionally, using the tools and techniques described here should be done with careful consideration of one’s own abilities.
These articles assume that the tasks will be performed by individuals with the necessary skills and knowledge to use the required tools safely, without risking harm to themselves, others, or property.
Therefore, the owners of this site cannot be held responsible for any direct or indirect damages resulting from the application of the content found here.
Before undertaking any DIY activity, it is strongly recommended to read the Safety Notice, which provide a basic (though non-exhaustive) list of precautions to follow.

Copyright

The republication of full articles is strictly prohibited. However, it is permitted to publish brief excerpts of the articles or images, provided that the source is clearly credited at the end. This must include a hyperlink directing readers to the original page from which the content was taken.

The site icon ilsitodelfaidate.it was downloaded from 123rf.com. © captainvector, 123RF Free Images

Disclaimer

Privacy Policy

Cookie Policy

Terms and Conditions

Consent Preferences