Many Ways to Blink 4: Ultrasonic Distance Sensor to Servo

This tutorial replaces the Light Sensor with a Ultrasonic Distance Sensor. This sensor sends out high frequency sound pulses and times how long it takes for them to come back much like a bat does. We can then convert that time into a distance. The interesting thing about this sensor is that it produces a digital signal as opposed to an analog signal. This type of sensor also has a a library called the NewPing library. This library takes the digital signals coming from the ultrasonic sensor and converts them into a the duration between when the ultrasonic sensor sent a pulse and when it received it back. Working with distances is easier than working with times so the library defines conversion factors that will convert this duration into a distance. In this tutorial we will use the “US_ROUNDTRIP_CM” variable which is equal to 57us/cm. Since US_ROUNDTRIP_CM already defined by the NewPing library we do not need to define it in our code.

#Block Diagram Ultrasonic Sensor to Servo System

Circuit Steps

Code

Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.

to

Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.

This set the Serial connection the right speed.

NewPing sonar(triggerPin, echoPin, maxDistance);
int maxDistance = 200;   // in centimeters

to the variable initialzation section of the code.

sensorValue = analogRead(sensorPin); 

to

delay(50);  // Wait 50ms between pings (about 20 pings/sec).
uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
distance = uS / US_ROUNDTRIP_CM;  // convert time to distance

The “delay(50)” guarantees that there will be enough time for the ultrasonic sensor to send and receive a signal before the next request comes. The sonar.ping() call returns?? its measurement in microseconds between when the pulse was sent out and when it was received. This is a little hard to understand so it is then converted to distance in centimeter.

unsigned int uS = 0; // holds the time it took for the pulse to be received
int distance = 0; // holds the distance in centimeters
int sensorValue = 0;  // variable to store the value coming from the sensor

and the line

int sensorPin = A0;    // select the input pin for the potentiometer    
Serial.print("Sensor value: "); Serial.println(sensorValue);

to

Serial.print("Sensor value: "); Serial.println(distance);
delayTime = map (sensorValue, minValue, maxValue, 200, 1023);

to

  delayTime = map (distance, minValue, maxValue, 200, 1023);
int maxValue = 200;    // in centimeter
int minValue = 5;      // in centimeter

Full Code

/*
  Adapted From: Analog Input by David Cuartielles and Tom Igoe
  Author: Malcolm Knapp
  Project: Ultrasonic Sensor to Servo 
  Date: 4/10/14
  Version: 0.1
  Description: This code shows how to use a Ultrasonic Distance Sensor to 
               control the "blink" rate of a servo. In this case "blink" means
               moving between two positions.

 */
// ---------- included libraries ------------ 
#include <Servo.h>
#include <NewPing.h>

// ---------- hardware pin defines  ----------- 
int triggerPin = 12;      // select the pin for ultrasonic trigger
int echoPin = 11;      // select the pin for echo

// ---------- variable initialization  ----------- 
int delayTime = 0; //variable that holds the delay time in milliseconds
int scaling = 1;
unsigned int uS = 0;      // holds the time it took for the pulse to be recived
unsigned int distance = 0; // holds the distance in centimeters
int maxValue = 200;    // in centimeter
int minValue = 0;      // in centimeter
int maxDistance = 200;   // in centimeters

// ---------- library initialization  -----------  
Servo myservo;  // create servo object to control a servo a maximum of eight servo objects can be created 
NewPing sonar(triggerPin, echoPin, maxDistance);

void setup() {
  Serial.begin(9600);
  // declare hardware connections
   myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  // Input
  delay(50);  // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  distance = uS / US_ROUNDTRIP_CM;  // convert time to distance
  // Debugging
  Serial.print("uS value: "); Serial.println(uS);
  Serial.print("Distance (cm): "); Serial.println(distance);

  // Processing 
  //Scaling
  delayTime = map (distance, minValue, maxValue, 200, 1023);
  Serial.print ("Delay in milliseconds: "); Serial.println (delayTime);
  // Modes
  // None - put new modes here

  // Output 
  myservo.write(155); 
  delay(delayTime); 
  myservo.write(30);
  delay(delayTime);    
}