# Measure Distance Traversed by a Robot With an Encoder

5,521

7

3

## Introduction: Measure Distance Traversed by a Robot With an Encoder

Encoders are very useful in the realm of mobile robotics. It's main application is to record the number of "steps" that a motor has taken. It is commonly paired with DC motors to be able to record how far a robot has gone and control it's motor accordingly. It is mostly used with a pin wheel of a set angle which will be used to calculate how many turns and in-turn the overall displacement of the wheels.

## Step 1: Tools and Materials

• Arduino 101 or Arduino Uno
• Encoder
• Jumper Wires
• LED
• 100Ω Resistor

## Step 2: Circuitry

Since the LED and Encoder are the only electrical components that needs to be powered, and both have different power requirements, there is no need to use the power rails of the breadboard.

Wiring the LED

• Connect the positive terminal of the LED to a 100Ω resistor in series to pin 9 of the Arduino
• Connect the other lead to the GND pin on the Arduino

Wiring the Encoder

• Following the labels on the encoder, connect VCC to the 5V pin on the Arduino.
• Connect the ground pin of the encoder to the GND pin on the Arduino
• Connect the signal pin of the encoder to the A0 pin of the Arduino

## Step 3: Code

LED test program:

void setup(){
pinMode (A0, INPUT); pinMode (9, OUTPUT); Serial.begin(9600); }

void loop(){

if (analogRead (A0) > 550 ){ digitalWrite (9, HIGH); } else{ digitalWrite (9, LOW); } }

Step counter program

#define photopin 8volatile byte interrupt;int total;
void setup() { // put your setup code here, to run once:pinMode (photopin, INPUT); attachInterrupt(0, interruptcount, RISING);interrupt = 0; Serial.begin(9600);} void interruptcount() { interrupt++;} void loop() { // put your main code here, to run repeatedly: delay(1000); detachInterrupt(0); total += interrupt; interrupt =0; attachInterrupt (0, interruptcount, RISING); Serial.print ("total count ="); Serial.println (total); /* int photovalue = digitalRead (photopin); Serial.print ("status: "); Serial.println(photovalue);if (photovalue > 0) { interrupt++; }Serial.print ("total interrupts: ");Serial.print (interrupt); */}

## Step 4: Demo

Every time that an object blocks the encoder, then the LED will light up, and a step will be counted. Using a calibrated pinwheel, we will be able to count the number of turns a wheel has taken. Consequently, we are able to calculate the distance travelled by the robot. This demo shows how to use the encoder, and shows that the LED lights up every time a step is taken. Participated in the
Makerspace Contest 2017