Csed Lab
Csed Lab
Csed Lab
ARDUINO
Arduino
• An open-source hardware platform based
on an Atmel AVR 8-bit microcontroller and
a C++ based IDE
• Over 300000 boards have been
manufactured
• Arduino Due is based on a 32-bit ARM
Cortex
Typical Arduino Board
Arduino IDE
Arduino Programming Basics
Comments
void setup() {
}
void loop() {
}
If Statements
if ( this is true ) { do this; }
If
if ( this is true ) { do this; }
Conditional
if ( this is true ) { do this; }
Action
if ( this is true ) { do this; }
Else
else { do this; }
Basic Repetition
•loop
•For
•while
Basic Repetition
void loop ( ) { }
Basic Repetition
void loop ( ) { }
Basic Repetition
void loop ( ) { }
void loop ( ) { }
void loop ( ) { }
void loop ( ) { }
Basic Repetition
while ( count<10 )
{
//while action code goes here
}
Basic Repetition
while ( count<10 )
{
//while action code goes here
//should include a way to change count
//variable so the computer is not stuck
//inside the while loop forever
}
Basic Repetition
while ( count<10 )
{
//looks basically like a “for” loop
//except the variable is declared before
//and incremented inside the while
//loop
}
Basic Repetition
Or maybe:
while ( digitalRead(buttonPin)==1 )
{
//instead of changing a variable
//you just read a pin so the computer
//exits when you press a button
//or a sensor is tripped
}
Important functions
• Serial.println(value);
– Prints the value to the Serial Monitor on your
computer
• pinMode(pin, mode);
– Configures a digital pin to read (input) or write (output)
a digital value
• digitalRead(pin);
– Reads a digital value (HIGH or LOW) on a pin set for
input
• digitalWrite(pin, value);
– Writes the digital value (HIGH or LOW) to a pin set for
output
Using LEDs
void setup()
{
pinMode(13, OUTPUT); //configure pin 13 as
output
}
// blink an LED once
void blink1()
{
digitalWrite(13,HIGH); // turn the LED on
delay(500); // wait 500 milliseconds
digitalWrite(13,LOW); // turn the LED off
delay(500); // wait 500 milliseconds
}
Switch Case
Setup, Interrupts
void setup ( ) {
attachInterrupt (interrupt, function,
mode) }
You can designate an interrupt
function to Arduino pins # 2 and 3
void setup() {
Serial.begin(9600);
}
50
Writing to the Console
void setup() {
Serial.begin(9600);
Serial.println(“Hello World!”);
void loop() {}
51
Reading data from Arduino
void setup()
{
Serial.begin(9600);
}
void serialtest()
{
int i;
for(i=0; i<10; i++)
Serial.println(i);
}
DC MOTORS AND
ACTUATORS
Actuators
• An electric motor is an electrical machine that
converts electrical energy into mechanical energy.
• Actuator: Device that turns energy (typically
electrical) to motion
• Features
– Force
– Speed
– Torque
– Efficiency
DC motor
• Force is produced
(F=ILB) due to the
electric current in a
wire inside a magnetic
field.
• Proportional to the
current, therefore can
be controlled by
potentiometer
• Hard to control
precisely
Buggy Motor Code
then wait to capture the rising edge output by echo port, at the
same time, open the timer to start timing.
PIC18F26K80 DEVELOPMENT BOARD
Ultrasonic Sensor Example (1/2)
void loop() {
int s=0;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceCm= (duration*0.034)/2;
distanceInch = (duration*0.0133)/2;
Serial.println(distanceCm);
if(distanceCm<30) //here we set the object range 15cm.
{
Put your code here
}
ZigBee Technology
The explosion in wireless technology has seen the emergence of many standards,
especially in the industrial, scientific and medical (ISM) radio band. There have been
a multitude of proprietary protocols for control applications, which bottlenecked
interfacing. Need for a widely accepted standard for communication between sensors
in low data rate wireless networks was felt.
Comparision Between Zigbee and Bluetooth