Tilt Sensing Servo Motor Controller using Arduino microcontroller

Tilt Sensing Servo Motor Controller using Arduino microcontroller

Sensors allow people to operate consumer and industrial products using physical stimuli such as touch, sound, and motion. In previews projects, you controlled two LEDs with the wave of your hand; the light-activated switch used a photocell to detect the presence of your hand over the sensor. In this chapter, we’ll build a gadget to easily detect object orientation using a tilt control switch to control a servo motor. This is an awesome device to build and show your Maker smarts to family and friends. By rotating the tilt control switch in an upright position, you’ll be able to operate a servo motor. Figure 3-1 shows the Tilt Sensing Servo Motor Controller.

Parts List

  1. Arduino microcontroller
  2. SW1: tilt control switch
  3. JI: servo motor
  4. R1: 1K ohm resistor (brown, black, red stripes)
  5. Pair of alligator test leads or equivalent
  6. Full-size clear breadboard
Tilt Sensing Servo Motor Controller using Arduino microcontroller
Figure 3-1. Tilt Sensing Servo Motor Controller built on a full-size clear breadboard

Let’s Build a Tilt Sensing Servo Motor Controller

You can control a servo motor’s rotation direction through orientation detection using a tilt control switch. In this project, you will build a Tilt Sensing Servo Motor Controller. Refer to the Parts List for all the electronic components required for this project. Here are the steps used to build the electronic device:

  1. From the Ultimate Microcontroller Pack, place the required parts on your workbench or lab tabletop.
  2. Assemble the servo motor with the appropriate mechanical assembly attachment, as shown in Figure 3-2 (left).
  3. Strip insulation from three ¼-inch solid wires and insert them into the servo motor’s mini connector, as shown in Figure 3-2 (right).
Tilt Sensing Servo Motor Controller using Arduino microcontroller
Figure 3-2. Servo motor with mechanical assembly attachment and modified servo motor wire connector (left); close-up of modified servo motor wire connector (right)
  1. Place and secure the servo motor on the full-size clear breadboard with hookup wire, as shown in Figure 3-3.
  2. Insert the modified servo motor wire connector into the full-size clear breadboard, as shown in Figure 3-4.
  3. Wire the electronic parts using the Fritzing diagram of Figure 3-5, or the actual project shown in Figure 3-1.

 

 

Tilt Sensing Servo Motor Controller using Arduino microcontroller
Figure 3-3. Placing and securing the servo motor on the full-size clear breadboard

 

Tilt Sensing Servo Motor Controller using Arduino microcontroller
Figure 3-4. Modified servo motor wire connector inserted into the full-size clear breadboard

 

Tilt Sensing Servo Motor Controller using Arduino microcontroller
Figure 3-5. Tilt Sensing Servo Motor Controller Fritzing diagram

 

Upload the Tilt Sensor Sketch

With the Tilt Sensing Servo Motor Controller circuit built on the full-size clear breadboard, it’s time to upload the sketch:

  1. Attach the Arduino to your computer using a USB cable.
  2. Open the Arduino software and type Example 3-1 into the software’s text editor.
  3. Upload the sketch to the Arduino.
  4. Rotate the tilt control switch back and forth. The servo motor will spin in the same direction as
  5. the tilt control switch orientation.

TROUBLESHOOTING TIP
Rotate the tilt control switch slowly and smoothly to get the best response from the servo motor.

Example 3-1. Tilt Control Switch sketch

/* This sketch controls a servo motor using a tilt control switch!
*
* 12 December 2012
* by Don Wilcher
*
*/
#include<Servo.h> // include Servo library
int inPin = 2; // the tilt control switch is wired to Arduino D2 pin
int reading; // the current reading from the input pin
Servo myservo; // create servo motor object
void setup()
{
myservo.attach(9); // attach servo motor to pin 9 of Arduino
pinMode(inPin, INPUT); // make pin 2 an input
}
void loop()
{
reading = digitalRead(inPin); // store digital data in variable
if(reading == HIGH) { // check digital data with target value
myservo.write(180); // if digital data equals target value,
// servo motor rotates 180 degrees
delay(15); // wait 15ms for rotation
}
else { // if reading is not equal to target value,
myservo.write(0); // rotate servo motor to 0 degrees
delay(15); // wait 15ms for rotation
}
}

Book Reference: Make: Basic Arduino Projects
26 Experiments with Microcontrollers and Electronics
By: Don Wilcher

Write Your Comments

comments

Leave a Reply