Notes of PhiL    About    Categories    Tags    Archive    Feed    GitHub Repo

Arduino Projects Book - Project 05: Mood Cue

Good evening! Today, I’ll continue the next project in the Arduino Projects Book, Mood Cue. In this project, we will learn how to control a servo motor using a potentiometer. By assembling a small indicator on the rotor of the servo, we can get the servo to point to different angles.





The headers that come with the Arduino Starter Kit are a bit short on one side and too long on the other. So, I had to use my fingers to push the plastic cover down to the middle of the headers so that the headers would fit the female socket of the servo.


The servo motor draws more current when it is in motion. This will cause a dip in voltage on the board. By placing a 100-microFarad capacitor across 5V and GND of the servo, we can smooth out any voltage changes that may occur.

Here is my schematic:


Here is my breadboard layout from Fritzing:


As always, these are available on my GitHub. Lastly, this is my circuit board:



In addition to starting the Serial Monitor and reading the analog and controlling the Servo Motor, map() is a function that helps remap a number from one range to another.

map(value, from_low, from_high, to_low, to_high);

A value of from_low will be mapped to to_low. A value of from_high will be mapped to to_high. A value in between from_low and from_high will be mapped to a different value between to_low and to_high. A great usage of this function is to map a value collected by a photocell (ranging between 0 and 1023) to a value that output to an RGB LED (ranging between 0 and 255): red_led_val = map(red_photo_val, 0, 1023, 0, 255); (remember the previous project?).

Next, we need to include the Servo library so that we can work on our servo motor easily. After we #include <Servo.h> and declare Servo MyServo, we provide the servo motor with attach() and write() methods.

#include <Servo.h>

Servo MyServo;

MyServo.attach(SERVO_PIN);  // the Arduino pin that controls the servo
MyServo.write(90);          // write the angle

Finally, see below for my project 05 code, or see it on GitHub.

 * Project Name: Arduino Projects Book - Project 05: Mood Cue
 * File Name: mood_cue.ino
 * Description: Expresses your feeling by a servo motor.
 * Author: Phi Luu
 * Location: Portland, Oregon, United States
 * Created: November 21, 2015
 * Updated: June 22, 2017

// include Servo library for servo motors
#include <Servo.h>

// Required hardware I/O connections
const byte POT_PIN   = A0;           // connect potentiometer to A0
const byte SERVO_PIN = 3;            // connect servo motor to ~3

// Servo class
Servo MyServo;                       // declare MyServo

// Global constants
const unsigned int BAUD_RATE = 9600; // serial monitor's baud rate

// Global variables
unsigned int  pot_val;               // potentiometer
unsigned char angle;                 // servo's angle

void setup() {
    MyServo.attach(SERVO_PIN);       // attach MyServo into SERVO_PIN
    Serial.begin(BAUD_RATE);         // start the Serial Monitor

void loop() {
    // read & print potentiometer value
    pot_val = analogRead(POT_PIN);
    Serial.print("pot_val: ");
    // map & print from pot value to angle
    angle = map(pot_val, 0, 1023, 0, 179);
    Serial.print(", angle: ");
    // run the servo by the angle
    // delay to stabilize



map() is a great function which helps mapping a value from one range to another. The <Servo.h> is also a useful library as it makes servo control much easier. I didn’t simulate the door’s mood indicator because it was hard to glue the servo on the door with the Arduino controlling it from the inside (I would possibly have to drill a hole on the door for wiring). But anyways, it’s been the fifth project of the Arduino Projects Book - Mood Cue.