Lab 4 Documentation by Amber Wang (Marcela)

Lab 4: Sensors

Date: March 4, 2017

Instructor: Marcela Godoy

Documented by Amber Wang

In this lab we are supposed to use a sensor to build our own circuit and use Arduino to trigger an output. The materials I used was: a vibration sensor, a servo, Arduino and breadboard.

This is the first time that we were not provided with already written codes and circuit diagram, and we were supposed to build our own circuit and write our own codes. At first I was very confused and didn’t know where to start. Jiwon helped me find a sample code in Arduino “Examples-Basics-AnalogReadSerial” which can read input from my sensor. I used this to test my sensor, and whenever I pressed the sensor, the numbers in serial monitor will have big change. The next step is to find an output which will transfer those numbers to a physical movement and achieve the interaction. I chose servo because we just learned it in Thursday’s class. For the servo and vibration sensor, both of them are “analog”. So I connected the sensor to analog input A0, and the servo to analog output  9. In my codes, I used a map function to map the range I got from the sensor 0-1023 to the angle that servo can rotate 0-180. Then I used a “write” function to let the servo rotate according to the input change in the sensor.

I met several problems during this process. The first thing was that because both servo and sensor has three wires connected to ground, power and pin, I had to connect both of them to 5v. I could not plug the two wires into same 5v slot in Arduino. Then I had to use the breadboard. However, the circuit still could not work. Then Jiwon helped me to debug. We first debugged the sensor by writing a “Serial.println” function which as I said before monitored the change in sensor and displayed it as numbers in serial monitor. It worked fine. Then we tested the servo by making it rotate without outside input. It worked as well. Then we found out it was because I forgot to connect the servo’s ground to the breadboard. So using a breadboard should be very careful that you have to make sure you connect all wires to the breadboard correctly!

The finished effect is shown in following video:

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int potpin = 0;  // analog pin used to connect the sensor
int val;    // variable to read the value from the analog pin

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object

void loop() {
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  Serial.println("mapped: "+ val);
  myservo.write(val);                  // sets the servo position according to the scaled value
  delay(500);                           // waits for the servo to get there

One thought on “Lab 4 Documentation by Amber Wang (Marcela)

  1. Hi Amber!
    great job, but two things.
    The servo motor is connected to a digital pin, but we will use one that is PWM or the one with the ” ~ ” before the number. This will give values between 0 and 255 by sending pulses of electricity.
    you might want to try embeding your video, but you need to change the mode of your post.

    thanks for posting the code in the right way though!

Leave a Reply