Vibration Motor

Vibration motor with touch sensor trigger

In this example we will demonstrate how to use a Touch Sensor and a Vibration Motor. This example is another demonstration of the use of the digital pins for both input and output. A digital signal is received from the Touch Sensor and is used to activate and deactivate the Vibration Motor.

This could be applied to the creation of a toy for children such as a robot cat that responds to touch by vibrating.

sku_392463_1

Required Components

  • SODAQ Mbili Board
  • 0.5W Solar Panel
  • 1aH Battery Pack
  • Grove – Touch Sensor
  • Grove – Vibration Motor
  • 2x Grove Cable (any length)

Required Libraries

  • none

Hardware Setup

You should refer to both the board diagram and Grove sockets page for additional information.

  1. First, plug the Vibration Motor into the socket for the digital pins D4 D5.
  2. Then, plug the Touch Sensor into the Grove socket for the digital pins D20 D21.
  3. Next, plug the 0.5W solar panel and the 1A LiPo battery into their respective sockets.

Turn on the SODAQ Mbili board, compile and upload the following sketch from the Arduino IDE onto the SODAQ Mbili board, and then unplug the USB cable from the computer when it has completed the upload.

Sketch Code

#define MOTOR_PIN 4 // Use digital pin 4 for the vibration motor
#define TOUCH_PIN 20    // Use digital pin 20 for the touch sensor

void setup() 
{
  //Set the digital pin modes
    pinMode(MOTOR_PIN, OUTPUT);
    pinMode(TOUCH_PIN, INPUT);
}

void loop() 
{
  //Read the current state of the touch sensor
    int sensorValue = digitalRead(TOUCH_PIN);

    if(sensorValue == HIGH)
    {
    //If the sensor is reading HIGH switch on the vibration motor
        digitalWrite(MOTOR_PIN, HIGH);
    }
    else
    {
    //If not, turn the vibration motor off
        digitalWrite(MOTOR_PIN, LOW);
    }
}

When you touch the sensor, you should feel the vibration motor activate.

Sketch Code Breakdown

Globals

Here we specify the digital pins we will use for both the Vibration Motor and the Touch Sensor.

#define MOTOR_PIN 4 // Use digital pin 4 for the vibration motor
#define TOUCH_PIN 20  // Use digital pin 20 for the touch sensor

setup()

Here we set the pin mode for the two digital pins we are using. This is done through a call to pinMode() with the first parameter specifying the pin to be set and the second parameter specifying the mode for that pin. The Vibration Motor pin is set to OUTPUT mode while the Touch Sensor pin is set to INPUT mode.

void setup() 
{
  //Set the digital pin modes
    pinMode(MOTOR_PIN, OUTPUT);
    pinMode(TOUCH_PIN, INPUT);
}

loop()

Here we read the state of the Touch Sensor by reading a value from the digital pin that it is connected to. This is done using the digitalRead() method which returns a value matching the built in constants of HIGH or LOW. We then modify the state of the output pin connected to the Vibration Motor using the digitalWrite() method, passing the value returned from the Touch Sensor. An if/else conditional is used to determine whether to switch the Vibration Motor on or off.

void loop() 
{
  //Read the current state of the touch sensor
    int sensorValue = digitalRead(TOUCH_PIN);

    if(sensorValue == HIGH)
    {
    //If the sensor is reading HIGH switch on the vibration motor
        digitalWrite(MOTOR_PIN, HIGH);
    }
    else
    {
    //If not, turn the vibration motor off
        digitalWrite(MOTOR_PIN, LOW);
    }
}