0

$USD

$USD
TUTORIALS Arduino

Arduino Intermediate Kit Tutorial 13: Open Sesame!

DFRobot Sep 01 2017 1449

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

COMPONENTS

To assemble this project you will need the following components:

1×  Digital Vibration Sensor
1×  TowerPro SG50 Servo
DFRduion UNO R3 (similar as Arduino UNO)
IO Expansion Shield for Arduino V7.1


HARDWARE CONNECTIONS
Connect the TowerPro SG50 to digital pin 9
Connect the Digital Vibration Sensor to digital pin 3


Review the diagram below for reference:




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, plug the Arduino in 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 not exceeding 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.

Shake the vibration 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

We use the <Servo.h> library

#include <Servo.h>   
This library is already included in the Arduino IDE. Here is the location of library <Servo.h>: Arduino-1.0.5/ libraries/ Servo/ Servo.h

We cannot use the functions in the library directly. We need to define an instance of the Servo. It would serve as a medium to connect our code with the library.

Servo myservo;

Once the connection has been set up, you can use functions from the library with the following method.

myservo.functionName();


Don’t forget the dot (“.”) in the middle.

We use the function “attach()” to define which pin the servo should be attached to.
attach(pin);

The function of attach() has one parameter – “pin”, which refers to one of the digital pins (we do not recommend digital 0 or 1 due to tx and rx interference). Here we have chosen digital pin 9.

myservo.attach(9);

How do we write the corresponding angle in the code?

write(pos);

pos represents the angle being written.


If you have any questions, please feel free to comment.
There is also another best selling arduino starter kit for you to choose.
Related category: arduino kits > education kits
Last Arduino Tutorial:  Arduino Intermediate Kit Tutorial 12: Temperature and Humidity Sensor
Next Arduino Tutorial: Arduino Intermediate Kit Tutorial 14: Pandora’s Box