Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 20

ARDUINO PROGRAMMING

WITH SENSORS, MOTORS AND


EXTERNAL INTERRUPTS

COURSE: Microprocessors and Microcontrollers-II


CONTACT: atiq@mail.au.edu.pk
CONTROLLING MOTORS USING
ARDUINO
DigitalOutput-Motion-Servo Motor
 Servo Motors are electronic devices
that convert digital signal to
rotational movement.

Functions to control Servo motor:

• Servo servoname; // used to name a servo

• attach(pin); //PWM pins are used , Fucntion called through servoname

• write(angle) //called through servoname

• read() //called through servoname, return current servo angle

• detach(); //detach servo. Pins can be used for PWM

3
Standard Servo Rotation to Exact Angel
//Code to rotate a servo motor attached with pin#9 in clock and anticlockwise direction continuosly
#include <Servo.h> //servo function library
Servo myservo; // create servo object to control a servo.
int pos = 0; // variable to store the servo position
void setup()
{ myservo.attach(9); // attaches the “myservo” on pin 9 to the servo object
}
void loop()
{ myservo.attach(9); //command to tell where “myservo” is attached
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell “myservo” to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
myservo.detach(); //Detach the myservo if you are not controling it for a while
delay(2000);
} 4
STEPPER MOTORS
 Motor controlled by a series of electromagnetic coils.
 The coils are alternately given current or not, creating magnetic
fields which repulse or attract the magnets on the shaft, causing
the motor to rotate.
 Allows for very precise control of the motor. It can be turned in
very accurate steps of set degree increments

Functions to control Stepper motor:

• Stepper motorname(steps, pin1, pin2, pin3, pin4)

• setSpeed(rpm) //called through motorname

• step(steps) // called through motorname


STEPPER MOTOR CONTROL
//code to rotate motor attached with pin 8,9,10,11, rpm=60 and 200 steps/rev

#include <Stepper.h> //the control sequence is in this library


const int stepsPerRevolution = 200; // motor-dependent
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); //pins used

void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60); //actually sets the delay between steps
}

void loop() {
// step one revolution in one direction:
myStepper.step(stepsPerRevolution);
delay(500);
}
USING EXTERNAL INTERRUPTS
USING EXTERNAL INTERRUPTS
 On a UNO Arduino board, two pins can be used as interrupts:
pins 2 and 3. and in MEGA these pins are used:2, 3, 18, 19, 20,
21

 The interrupt is enabled through the following line:


attachInterrupt(digitalPinToInterrupt(pin), ISR, mode);

 modes are LOW, HIGH, RISING, FALLING, CHANGE

 E.g
attachInterrupt(digitalPinToInterrupt(2), doEncoder, FALLING);
//Interrupt will occur on FALLING edge and doEncoder function called when
falling edge occurs at pin 2
INTERRUPT EXAMPLE
//code to toggle LED status attached with PIN9 whenever pin 3 status
changes.
int led = 9;
volatile int state = LOW;
void setup()
{
pinMode(led, OUTPUT);
attachInterrupt(digitalPinToInterrupt(3), blink, CHANGE);
//PIN 3 for Intr , blink function called when change on pin 1 occurs
}
void loop()
{
digitalWrite(led, state);
}
void blink()
{
state = !state;
}
USING TIMER INTERRUPTS
USING TIMER INTERRUPT FUNCTIONS
 initialize(period) ; //period is given in microseconds
You must call this function first to use any of the other
functions. You can optionally specify the timer's period here
(in microseconds), by default it is set at 1 second.
Note: This breaks analogWrite() for digital pins 9 and 10 on
Arduino.

 attachInterrupt(function) //
Calls a function at the specified interval in microseconds.
Code: Toggle LED attached with pin#10 of Arduino after every half second using
timerone interrupt?

#include "TimerOne.h"
void setup()
{
pinMode(10, OUTPUT);
Timer1.initialize(500000); // initialize timer1, and set a 1/2 second period
Timer1.attachInterrupt(callback); // attaches callback() as a timer overflow interrupt
}
void callback()
{
digitalWrite(10, digitalRead(10) ^ 1);
}
void loop()
{
// your program here...
}
SENSORS!
Sound-Piezo Sensor

 A Piezo is an electronic piece that converts electricity


energy to sound.

 It is a digital output device.

 You can make white noise or even exact musical notes


(frequencies for musical notes) based on the duration that
you iterate between HIGH and LOW signals.

14
Arduino- Digital Output-Sound-Piezo
// Program to play musical tones using piezo sensor attached with pin 13
int freqs[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};

void setup(){
pinMode(13,OUTPUT);
}

void loop(){
for(int i=0;i<8;i++) //iterating through notes
{
for(int j=0;j<1000;j++) //the time span that each note is being played
{
digitalWrite(13,HIGH);
delayMicroseconds(freqs[i]); //delay in microseconds
digitalWrite(13,LOW);
delayMicroseconds(freqs[i]);
}
}
} //loop ends here
15
ULTRA SONIC SENSOR ASSIGNMENT
Using Ultra Sonic Sensor, Measure distance
of an object and display it on serial port?

Approach:
1.Understand how this sensor works
2.Understand mathematical sense of pulse generated by this
sensor
3.Understand pulseIn() function , it will help you to get pulse width
4.Code it and test it in proteus
ULTRA SONIC SENSOR INTRO

 It emits an ultrasound at 40000 Hz which travels


through the air and if there is an object or
obstacle on its path, It will bounce back to the
module.

 It has 4 pins
 Ground,VCC, Trig and Echo

17
ULTRA SONIC SENSOR WORKING
 To generate the ultrasound you need
to do:
1. Set the Trig on a High State for
10 µs.
2. That will send out an 8 cycle
sonic burst which will travel at
the speed sound and it will be
received in the Echo pin.
3. The Echo pin will output the
time in microseconds the
sound wave traveled from TX
to RX

18
How to measure distance?
 Speed of the sound is 340 m/s or 0.034
cm/µs
 Below is an example for distance= 10cm
 In Arduino function pulseIn(pin#,value)
is used to measure width of pulse in
microseconds. Value is HIGH or LOW.
i.e starting point of pulse!

19
void loop() {
// Clears the trigPin
Scenerio: Trigger pin of ultrasonic sensor is digitalWrite(trigPin, LOW);
attached with pin9 and echo with pin 10 of delayMicroseconds(2);
Arduino. On serial port throw the distance of
an object that comes in range of this // Sets the trigPin on HIGH state for
sensor? 10us
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
// defines pins numbers
digitalWrite(trigPin, LOW);
const int trigPin = 9;
const int echoPin = 10; // Reads the pulse width of echoPin, i.e
returns the sound wave travel time in us
// defines variables duration = pulseIn(echoPin, HIGH);
long duration;
int distance; // Calculating the distance
distance= duration*0.034/2;
void setup() {
pinMode(trigPin, OUTPUT); // trigPin as an o/p // Prints the distance on the Serial
pinMode(echoPin, INPUT); // echoPin as an i/p monitor
Serial.begin(9600); // Starts the serial comm Serial.print("Distance: ");
} Serial.println(distance);
}

You might also like