Microcontroller Basics: Dizzy Woodpecker

Learning about microcontroller was a bit overwhelming. But it was fun though. This time I was exploring basic Arduino: digital and analog input. It was confusing at first, specifically in the coding part. But after time, I could understand a little bit more how it worked.

After doing some labs about digital & analog input-output, servo motor, and sensors, I tried to make this Dizzy Woodpecker. You can control the Woodpecker using hand gesture.

Here, I used Servo Motor and Infrared Distance Sensor.

I used Sharp 2Y0A21 IR distance sensor. It measures the distance by shining a beam of infrared light and uses a phototransitor to measure the intensity of the light that bounces back (source: create.arduino.cc).

For the Servo Motor, I used TowerPro SG92R. This servo motor is a device that can turn into a specific direction. It can be plugged with an arm that can turn from 0 to 180 degree.

Then I built a circuit using the Servo Motor and IR distance sensor. Here’s the sketch.

Here’s what it looks like IRL.

Then I coded the microcontroller using Arduino IDE on desktop. I realized that the distance value is a range between 0 to somewhere around 500, but I wasn’t sure. And since the servo can only rotates between 0 to 180 degrees, so I mapped the distance value to rotation value. This is what I did.

#include &amp;amp;amp;lt;Servo.h&amp;amp;amp;gt;

Servo servoMotor;
const int servoPin = 3; // digital input pin
int analogValue; // value from analog input
int servoAngle; // servo rotation degree

void setup() {
Serial.begin(9600); // setup communication to computer
servoMotor.attach(servoPin); // setup servo
}

void loop() {
analogValue = analogRead(A0); // store input value
servoAngle = map(analogValue, 0, 1023, 0, 179); // map distance value to rotation value
Serial.println(servoAngle); // print to computer
servoMotor.write(servoAngle); // output to servo
}

Here’s my Dizzy Woodpecker!