Вы находитесь на странице: 1из 49

www.ptcdb.edu.

ps Arduino –Interfacing LAB

Arduino in the Interfacing Lab

Eng. Sa’id Ibrahom


www.ptcdb.edu.ps Arduino –Interfacing LAB
www.ptcdb.edu.ps Arduino –Interfacing LAB

What is Arduino?
• Arduino is an open source physical computing platform.
• based on a simple input/output (I/O) board.
• Arduino is a computation tool for sensing and controlling
signals.
• It is more convenient and cost effective than using a
personal computer PC.
• It's an open-source system in terms of hardware and
software.
• You can download the Integrated Development Environment (IDE) for your
own OS from http://arduino.cc/en/Main/Software
•Follow the instruction to install the IDE
www.ptcdb.edu.ps Arduino –Interfacing LAB

• Microcontroller is based on ATmega328 14 digital input/output (I/O) pins


13,12,… ………2,1,0
• 14 digital input/output (I/O) pins plus
• 6 analog input pins (these pins can also be
programmed to be digital I/O pins)
• A 16 MHz ceramic resonator
• 28 Pins
• Powered by 3 or 5 Volts A0-A5
6 Analog inputs, they can also be used
• Requires about 0.1 Watts of power as digital I/O pins

• 32 KB of flash storage
• 2 KB of RAM
www.ptcdb.edu.ps Arduino –Interfacing LAB

Which Arduino?
Arduino UNO which is the classic Arduino, by far the most popular and is what 99% of projects use. It's
basic, well supported, and is a great starter
www.ptcdb.edu.ps Arduino –Interfacing LAB
www.ptcdb.edu.ps Arduino –Interfacing LAB

Interactive Device
The interactive device is an electronic circuit that
• is able to sense the environment using sensors
• processes the information from the sensors with
software (code)
• will then be able to interact with the environment
using actuators

Getting Started with Arduino


www.ptcdb.edu.ps Arduino –Interfacing LAB

SENSORS ACTUATORS
Light or infrared sensors Light sources (LED, LED RGB)
• servomotor

Environment sensors (humidity Infrared and laser sources


temperature and atmospheric
pressure)
Sound sensors Sound generators (buzzer)
Motion sensors Motion generators (servomotor)
Magnetic field sensors
Gas sensors
www.ptcdb.edu.ps Arduino –Interfacing LAB

Start to use the


Arduino IDE
• To start Arduino IDE,
click Start Menu  All
Programs  Arduino

• Make sure the board


model (Arduino Uno)
and connected port
(depends on your PC)
are correct
Your board Model The port that your
board connected to
www.ptcdb.edu.ps Arduino –Interfacing LAB

Select Board
www.ptcdb.edu.ps Arduino –Interfacing LAB

Select Port

Select a correct port.


The actual number depends on
your system.
www.ptcdb.edu.ps Arduino –Interfacing LAB

Arduino IDE
Tool Bar Serial monitor,
Can use this to issue
commands to the
board, and read
outputs from the
board.
This programming Area
is called “Sketch”.

The program code are placed here.

Status Message

Messages from the system


www.ptcdb.edu.ps Arduino –Interfacing LAB

• Verify
• Checks code for errors
• Upload
• Compiles and uploads code to the Arduino I/O board
• New
• Creates a new sketch
• Open
• Open sketch
• Save
• Save sketch
• Serial Monitor
• Display serial data being sent from the Arduino board
www.ptcdb.edu.ps Arduino –Interfacing LAB

Structure of any Arduino code


Before going to the setup function constant variables should be
int pin = 1; 1. Define Variables
defined

void setup() Setup function is run once, when the microcontroller boots up
2. Setting up functions
{} or resets.

After setup function the processor moves on to run code inside


void loop() the loop function. Code inside loop function will be run over
{} 3. Eternal loop
and over until the microcontroller is shut down.

• It’s required to have both setup() and loop() functions in the code
www.ptcdb.edu.ps Arduino –Interfacing LAB

Arduino Code

To run a program in Arduino, your sketch should


contain two methods
void setup()
{
// initialization of variables, pin modes, libraries
// run once after each power up or reset
}

void loop()
{
// loops the content consecutively
// allowing the program to change and respond
}
www.ptcdb.edu.ps Arduino –Interfacing LAB

Basic software functions


• Hardware related
• pinMode(), setup the functions of hardware pins
• digitalWrite(), set a pin to a digital level : ‘1’ or ‘0’
• digitalRead(), read the digital level of a pin: ‘1’ or ‘0’
• delay()
• Software related
• If-then-else
• For
• Switch-case
www.ptcdb.edu.ps Arduino –Interfacing LAB

System setup procedures

• (Step 1) Setup the direction of the pins:


• using pinMode(),
• (Step 2) Then you can set a pin to : HIGH or LOW
• (Step 2a) digitalWrite(), //set pin to : HIGH ‘1’ or LOW ‘0’
• or
• (step 2b) digitalRead(), //read state of pin: HIGH ‘1’ or LOW ‘0’
www.ptcdb.edu.ps Arduino –Interfacing LAB

Basic Function (step1) – pinMode()


• pinMode() is used to configure the specified pin to behave either as an input
or output, or input_pullup
• Syntax Pin =0,..,13, or A0,A1,..,A5 Write comment for you to read
for Digital I/O, or
pinMode(pin, mode) // comment
• pin: the index number of the pin whose mode you wish to set
• mode: INPUT, OUTPUT, INPUT_PULLUP
• Example:
• pinMode(1, OUTPUT)//setup pin1 =digital out
• pinMode(3, INPUT)//setup pin3 =digital in
• pinMode(A3, INPUT)//setup A3 for digital in
• pinMode(A3, OUTPUT)//setup A3 for digital out
• If no PinMode applied to A0->A5, they are analog_in by default.
www.ptcdb.edu.ps Arduino –Interfacing LAB

Meaning of INPUT, OUTPUT, INPUT_PULLUP

• INPUT: HIGH(5V) or LOW(0V)


Arduino

• OUTPUT:
HIGH(5V) or LOW (0V) Arduino

High(5V))
• INPUT_PULLUP:
1KΩ
When the pin is not Arduino
connect to anything, HIGH(5V) or LOW)
or
it is HIGH not_connected_to_anything
www.ptcdb.edu.ps Arduino –Interfacing LAB

Basic Function(step2a) – digitalWrite()

• digitalWrite() is used to write a HIGH or a LOW


value to a digital pin

• Syntax
digitalWrite(pin, value) // comment
• pin: the number of the pin whose value you wish to set
• value: HIGH (5 V) or LOW (Ground)
• Example:
• digitalWrite(pin, value) // comment
• E.g
• digitalWrite(1, HIGH)//set pin1 to HIGH
www.ptcdb.edu.ps Arduino –Interfacing LAB

Basic Function(step2b) – digitalRead()

• digitalWrite() is used to read the value from a


specified digital pin, either HIGH or LOW

• Syntax
digitalRead(pin)
• pin: the number of the pin whose mode you want to
read (integer)
• Example:
• digitalRead(pin)// read the state of the
• // it can be “HIGH” or “LOW”
www.ptcdb.edu.ps Arduino –Interfacing LAB

Some other basic Function – delay()

• delay() is used to pause the program for the


amount of time (in milliseconds)

• Syntax
delay(ms)
• ms: the number of milliseconds to pause (unsigned long)
www.ptcdb.edu.ps Arduino –Interfacing LAB

Basic Control Structure – IF

• Syntax
IF(condition1){
// do stuff if condition1 is true
}ELSE IF (condition2){
// do stuff only if condition1 is false
// and conition2 is true
}ELSE{
// do stuff when both condition1 and
// condition2 are false
}
www.ptcdb.edu.ps Arduino –Interfacing LAB

Basic Control Structure – FOR

FOR(initialization; condition; increment){


// statement(s);
}
www.ptcdb.edu.ps Arduino –Interfacing LAB

Basic Control Structure


– SWITCH-CASE
• switch (var) {
• case label1:
• // statements when var=label1
• break;
• case label2:
• // statements when var=label2
• break;
• default:
• // statements
•}
www.ptcdb.edu.ps Arduino –Interfacing LAB

Breadboard Layout
www.ptcdb.edu.ps Arduino –Interfacing LAB

CUBLOC Study Board


www.ptcdb.edu.ps Arduino –Interfacing LAB

How to get a 5V DC output from a 9V or 12V Supply


www.ptcdb.edu.ps Arduino –Interfacing LAB

Interfacing with an Arduino

• Serial Rx, Tx Line (UART)


• Serial Peripheral Interface (SPI)
• Inter-Integrated Circuit (I2C)
• External Interrupts (falling or rising edges)
• Pulse Width Modulation (PWM)
• Reset Pin
www.ptcdb.edu.ps Arduino –Interfacing LAB

Blink a LED
There are a lot of different LEDs available on the market.
Different LED characteristics include
• colors light / radiation wavelength,
• light intensity,
• a variety of other LED characteristics.
LED voltage drops
Typically the LED voltage drop is between around 2 and 4 volts.

Voltage drop (V)


RED 1,6
YELLOW 2,2
GREEN 2,4
WHITE (warm) 3,0
WHITE (cold) 3,5
www.ptcdb.edu.ps Arduino –Interfacing LAB

LEDs and Resistors

LED needs a current limiting resistor


(V - VLED )
Current for Light Emitting Diodes = 10/15mA R=
I
R=220 Ω (a good and practical compromise)

http://www.instructables.com/id/Choosing-The-Resistor-To-Use-With-LEDs/
www.ptcdb.edu.ps Arduino –Interfacing LAB

ATTENTION! PIN13

On pin 13, a resistance of 1 K Ohm is already integrated in the board,


so no additional resistance is required
www.ptcdb.edu.ps Arduino –Interfacing LAB

In order to determine the resistance of


a colour coded resistor it is necessary to
know the number that corresponds to
the various colours.
This information is obtained from a
Resistor Colour Code Chart.
www.ptcdb.edu.ps Arduino –Interfacing LAB

Example 1 - Blinking led

You need
• Breadboard
• Led
• Resistor (220 ohm)
• Arduino
• Wires
www.ptcdb.edu.ps Arduino –Interfacing LAB

Writing your first program: Basic blinking LED


int ledPin = 9;

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

void loop()
{
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}
www.ptcdb.edu.ps Arduino –Interfacing LAB

Writing your first program: Basic blinking LED


int ledPin = 13; //Variable to store the pin
number •Code is case sensitive
•Statements are commands and
void setup()
{ must end with a semi-colon
pinMode(ledPin, OUTPUT); //set ledPin as •Comments follow a // or begin
output
} with /* and end with */

void loop()
{
digitalWrite(ledPin, HIGH); //LED ON You may change the blinking time
delay(1000); //Wait 1000ms (=1s)
digitalWrite(ledPin, LOW); //LED OFF
delay(1000); //Wait 1000ms (=1s)
}
www.ptcdb.edu.ps Arduino –Interfacing LAB

Uploading the program

1. Click Verify
The code is checked for syntax errors

2. Click Upload
The program is uploaded to the Arduino mCu board
www.ptcdb.edu.ps Arduino –Interfacing LAB

The Traffic Light Controller


You need
• Red, yellow and green LEDs.
• A breadboard.
• 3 x 220 Ω resistors.
• Connecting wires.

You could modify the previous blinking sketch


www.ptcdb.edu.ps Arduino –Interfacing LAB

The Traffic Light Controller

int Green=9; //inizializziamo le variabili void loop() { // da qui tutto si ripete all'infinito accende e poi spegne
int Yellow=11; //assegnando i tre pin alle porte digitali da 9 a 11 sequenzialmente i tre led
int Red=13; digitalWrite(Red, HIGH);// attiviamo il primo pin high=acceso
int Pausa = 6000; // variabile intera corrispondente a 1000 in delay delay (Pausa); //pausa di 6000 ms cioè 6 s
stop per 1 sec digitalWrite(Red, LOW);// disattiviamo il primo pin low=spento
digitalWrite(Green, HIGH);// ora ripetiamo con il secondo pin
void setup(){ //definisco tutti i pin in modalità output-lo faccio una delay (Pausa);
volta sola all'inizio digitalWrite(Yellow, HIGH);// attiviamo il primo pin high=acceso
delay (2000); //pausa di 2000 ms cioè 2 s
pinMode (Green,OUTPUT);
pinMode (Yellow,OUTPUT); digitalWrite(Green, LOW);
pinMode (Red,OUTPUT); digitalWrite(Yellow, LOW);
} }
www.ptcdb.edu.ps Arduino –Interfacing LAB

LDR: Light Dependent Resistor (Photoresistor)

It’s an analog sensor


It needs a 10kΩ resistor, to limit the current
The variable resistance range will be∞
• 0Ω (when the light intensity is maximum)
• ∞ (when the light intensity is minimum)
www.ptcdb.edu.ps Arduino –Interfacing LAB

LDR _ the sketch


const int SegnalePin = A0; //pin a cui si collega il "segnale"

const int Pausa = 200; //valore della pausa tra 2 successive misurazioni

int Segnale = -1; //dichiarazione della variabile e inizializzazione


//ad un valore di controllo (impossibile da ottenere
//nella misura: se si ottiene in output è indice di errore) !!!Control that this
number is equal to the
void setup(){ number at bottom right
Serial.begin(9600); //Inizializzazione monitor seriale in the serial monitor
} window
void loop(){ Results (in V)
Segnale = analogRead(SegnalePin); //Lettura del Segnale are printed in serial monitor
Serial.println(Segnale); //Stampa su monitor seriale:

delay(Pausa);
}
www.ptcdb.edu.ps Arduino –Interfacing LAB

Sensor + Actuator: LDR+LED

We will turn on a LED when the


illumination value falls below a fixed
level

IF/ELSE
The if statement executes a command/statement if a
specified condition is true.
If the condition is false, another command/statement
can be executed.

if (pinFiveInput < 500)


{
// action A
}
else
{
// action B
}
www.ptcdb.edu.ps Arduino –Interfacing LAB

const int SegnalePin = A0; //pin a cui si collega il "segnale"


const int LedPin = 8; //pin a cui collegare il LED
const int Soglia = 400; //valore di riferimento di soglia:
//DEVE ESSERE CALIBRATO SULLA SPECIFICA SITUAZIONE //Stampa su monitor seriale:
// se necessario sostituite 400 con il valore da voi ottenuto Serial.println(Segnale);

const int Pausa = 200; //valore della pausa tra 2 successive misurazioni //Uso come ATTUATORE: da "scommentare" se utilizzato
if(Segnale < Soglia){
int Segnale = -1; //dichiarazione della variabile e inizializzazione digitalWrite(LedPin, HIGH);
//ad un valore di controllo (impossibile da ottenere } else {
//nella misura: se si ottiene digitalWrite(LedPin, LOW);
in output è indice di errore) }

void setup(){ delay(Pausa);


Serial.begin(115200); //Inizializzazione monitor seriale }
pinMode(LedPin, OUTPUT); //Dichiarazione del pin a cui è collegato il LED
come Output:

void loop(){
//Lettura del Segnale:
Segnale = analogRead(SegnalePin);
www.ptcdb.edu.ps Arduino –Interfacing LAB

Light intensity vs distance

Light source=smartphone flash


LDR
Activity
• change the distance between source and LDR
• read serial monitor
• plot ddp vs distance

This activity couldbe used to test Lambert Law

1
Iµ 2
r
www.ptcdb.edu.ps Arduino –Interfacing LAB

Our students have done this experiment


and these are their results
www.ptcdb.edu.ps Arduino –Interfacing LAB

Thermistor 10K: thermal resistor


In thermistors the resistance drastically changes with
temperature (change of 100 ohms or even more per degree!)
There are two kinds of thermistors:
• NTC (negative temperature coefficient)
• used for temperature measurement
• PTC (positive temperature coefficient).
• used as resettable fuses
www.ptcdb.edu.ps Arduino –Interfacing LAB

Thermistor 10K: thermal resistor


THERMISTORS HAVE SOME ADVANTAGES OVER OTHER KINDS OF TEMPERATURE SENSORS:

• First they are much cheaper than any other one 0,80 Euro
• They are also much easier to waterproof since its just a resistor.
• They work at any voltage (digital sensors require 3 or 5V logic).
• Compared to a thermocouple, they don't require an amplifier to read the
minute voltages - you can use any microcontroller to read a thermistor.
• They can also be incredibly accurate for their price. (Assuming you have an
accurate enough analogue converter)
• They are difficult to break or damage
www.ptcdb.edu.ps Arduino –Interfacing LAB

Thermistor 10K: thermal resistor


THE FIRST SKETCH: termistore 10k normale
// the value of the 'other' resistor
#define SERIESRESISTOR 10000

// What pin to connect the sensor to


#define THERMISTORPIN A0
Serial.print("Analog reading");
void setup(void) {
Serial.println(reading);
Serial.begin(9600);
}
// convert the value to resistance
reading = (1023 / reading) - 1;
void loop(void) {
reading = SERIESRESISTOR / reading;
float reading;
Serial.print("Thermistor resistance");
Serial.println(reading);
reading = analogRead(THERMISTORPIN);
delay(1000);
}
www.ptcdb.edu.ps Arduino –Interfacing LAB

When doing analog readings, especially with a 'noisy' board like Arduino, we suggest two tricks to improve results:
A) use the 3.3V voltage pin as an analog reference;
B) take a bunch of readings in a row and average them.

Thermistor_aref2.ino

Finally, of course, we want to have


the temperature reading, not just
a resistance!
• you can simply use the
temperature/resistance table
(calibration)
• you probably want actual
temperature values. to do that
we’ll use the Steinhart-Hart
Law

Вам также может понравиться