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: