Академический Документы
Профессиональный Документы
Культура Документы
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
• 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
SENSORS ACTUATORS
Light or infrared sensors Light sources (LED, LED RGB)
• servomotor
Select Board
www.ptcdb.edu.ps Arduino –Interfacing LAB
Select Port
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”.
Status Message
• 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
void setup() Setup function is run once, when the microcontroller boots up
2. Setting up functions
{} or resets.
• It’s required to have both setup() and loop() functions in the code
www.ptcdb.edu.ps Arduino –Interfacing LAB
Arduino Code
void loop()
{
// loops the content consecutively
// allowing the program to change and respond
}
www.ptcdb.edu.ps Arduino –Interfacing LAB
• 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
• 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
• 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
• Syntax
delay(ms)
• ms: the number of milliseconds to pause (unsigned long)
www.ptcdb.edu.ps Arduino –Interfacing LAB
• 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
Breadboard Layout
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.
http://www.instructables.com/id/Choosing-The-Resistor-To-Use-With-LEDs/
www.ptcdb.edu.ps Arduino –Interfacing LAB
ATTENTION! PIN13
You need
• Breadboard
• Led
• Resistor (220 ohm)
• Arduino
• Wires
www.ptcdb.edu.ps Arduino –Interfacing LAB
void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}
www.ptcdb.edu.ps Arduino –Interfacing LAB
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
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
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
const int Pausa = 200; //valore della pausa tra 2 successive misurazioni
delay(Pausa);
}
www.ptcdb.edu.ps Arduino –Interfacing LAB
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.
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 loop(){
//Lettura del Segnale:
Segnale = analogRead(SegnalePin);
www.ptcdb.edu.ps Arduino –Interfacing LAB
1
Iµ 2
r
www.ptcdb.edu.ps Arduino –Interfacing LAB
• 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
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