0

$USD

$USD
PROJECTS Arduino

Arduino Projects 9: Open Sesame

DFRobot Jun 22 2017 1788

In this session we are going to use servos to make a door move within this Gravity: Stater Kit for Arduino. The door can’t be opened easily – you will have to complete a challenge before it does!
The challenge will be explained below…

Parts Needed:

DFRduino Uno  (similar as Arduino UNO R3) x1
I/O Sensor Expansion Shield V7.1  x1
Digital Tilt Sensor  x1 

TowerPro SG50 Servo  x1 

Connections

Connect the TowerPro SG50 to digital pin 9 of DFRduino UNO (as arduino UNO)
Review the diagram below for reference:

Connect the Digital Tilt Sensor to digital pin 3 DFRduino UNO (as arduino UNO)


Be sure that your power, ground and signal connections are correct or you risk damaging your components.

When you have connected the components and checked your connections, connect the microcontroller to your PC with the USB cable so you can upload a program.


Code Input

#include <Servo.h>
int sensorPin = 3; // Digital Vibration Sensor-Digital 3
Servo myservo;
int pos = 0;
void setup() {
Serial.begin(9600);
pinMode(sensorPin, INPUT);
myservo.attach(9); // Servo– Digital 9
}
void loop() {
int sensorState = digitalRead(sensorPin); //Read state of the digital vibration sensor
Serial.println(sensorState);
if (!sensorState) { //If the state is 0, The servo moves 2° more but                                                    does not exceed 180°
pos = pos + 2;
if (pos >= 180) {
pos = 180;
}
myservo.write(pos); //Write in rotation angle of the Servo
Serial.println(pos); // Print rotation angle in serial port
delay(100);
} else { // Else, the servo moves 2° less but not less than 0°.
pos = pos - 2;
if (pos <= 0) {
pos = 0;
}
myservo.write(pos);
Serial.println(pos);
delay(100);
}
delay(1);}

Use this sample code to implement the behavior we want.

You can copy and paste it in to the Arduino IDE, but if you want to develop you skills we recommend typing it out.

When you have finished, click “Verify” to check the code for syntax errors. If the code verifies successfully, you can upload it to your arduino microcontroller.

Shake the tilt sensor and you‘ll see the servo’s rotation angle gradually increase. When you stop shaking, the servo’s rotation angle will decrease like a door slowly closing.


Code Analysis

Here we have used the <Servo.h> library
#include <Servo.h>
This library is included in Arduino IDE. The default location of <Servo.h> should be: Arduino-1.0.5/ libraries/ Servo/ Servo.h
We cannot use the functions in the library directly. We need to create a servo object in code
Servo myservo;
myservo.functionName();
We use the function attach() to define which pin the servo should be controlled by.
attach(pin);
interference). Here we have chosen digital pin 9.
myservo.attach(9);
How do we write the corresponding angle in the code?
write(pos);

You will be able to find it in the Arduino IDE menu bar: Sketch > Import Library > Servo. It would serve as a medium to connect our code with the library.

Once the servo object is declared, you can use functions from the library with the following method:

Don’t forget the . between myservo and f unctionName

The attach () function has one parameter – pin , which refers to one of the digital pins (we do not recommend using digital 0 or 1 due to TX and RX



pos represents the angle we want the servo to turn to.

There is also another arduino starter kit for you to choose, you could click arduino kit to find more.