Introduction to Arduino
Software Icon
Adding control – let’s use the Arduino
and start programming!!!
digitalWrite()
BIG 6 CONCEPTS
analogWrite()
digitalRead()
if() statements / Boolean
analogRead()
Serial communication
This work is licensed under a
Creative Commons Attribution-ShareAlike 3.0 United States License.
Concepts: INPUT vs. OUTPUT
Referenced from the perspective of the microcontroller (electrical board).
Inputs is a signal / information Output is any signal exiting the
going into the board. board.
Examples: Buttons Switches, Light Examples: LEDs, DC motor, servo
Sensors, Flex Sensors, Humidity motor, a piezo buzzer, relay, an RGB
Sensors, Temperature Sensors… LED
Arduino IDE
Integrated Development Environment (IDE)
Two required functions /
methods / routines:
void setup()
{
// runs once
}
void loop()
{
// repeats
error & status messages }
Settings: Tools Serial Port
•Your computer communicates
to the Arduino microcontroller
via a serial port through a
USB-Serial adapter.
•Check to make sure that the
drivers are properly installed.
Settings: Tools Board
•Next, double-check that the proper board is selected under the
ToolsBoard menu.
Arduino & Arduino Compatible Boards
digitalWrite()
Let’s get to coding…
•Project #1 – Blink
–“Hello World” of Physical Computing
• Psuedo-code – how should this work?
Turn LED Turn LED Rinse &
Wait Wait
ON OFF Repeat
comments
Three commands to know…
• pinMode(pin, INPUT/OUTPUT);
• ex: pinMode(13, OUTPUT);
• digitalWrite(pin, HIGH/LOW);
• ex: digitalWrite(13, HIGH);
• delay(time_ms);
• ex: delay(2500); // delay of 2.5
sec.
• // NOTE: -> commands are CASE-sensitive
Try adding other LEDs
•
Programming Concepts: Variables
Variable Scope
•Global
•---
•Function-level
Programming Concepts: Variable Types
• Variable Types:
8 bits 16 bits 32 bits
byte int long
char unsigned int unsigned long
float
analogWrite()
Fading in and Fading Out
(Analog or Digital?)
• A few pins on the Arduino allow for us to
modify the output to mimic an analog signal.
• This is done by a technique called:
• Pulse Width Modulation (PWM)
Concepts: Analog vs. Digital
•To create an analog signal, the microcontroller uses a
technique called PWM. By varying the duty cycle, we
can mimic an “average” analog voltage.
•Pulse Width Modulation (PWM)
Project #2 – Fading
Introducing a new command…
•analogWrite(pin, val);
•
•pin – refers to the OUTPUT pin
(limited to pins 3, 5, 6, 9, 10, 11.) –
denoted by a ~ symbol
•val – 8 bit value (0 – 255).
• 0 => 0V | 255 => 5V
Move one of your LED pins over to Pin 9
• In Arduino, open up:
• File Examples [Link] Fade
Fade - Code Review
Fade - Code Review
Project# 2 -- Fading
• Challenge 2a – Change the rate of the fading
in and out. There are at least two different
ways to do this – can you figure them out?
• Challenge 2b – Use 2 (or more) LEDs – so that
one fades in as the other one fades out.
Input
Input is any signal entering an electrical system .
• Both digital and analog sensors are forms of input
• Input can also take many other forms: Keyboards, a mouse,
infrared sensors, biometric sensors, or just plain voltage from
a circuit
Digital Input
• Connect digital input to your Arduino using Pins # 0 – 13
(Although pins # 0 & 1 are also used for programming)
• Digital Input needs a pinMode command:
• pinMode (pinNumber, INPUT);
• Make sure to use ALL CAPS for INPUT
• To get a digital reading:
• int buttonState = digitalRead
(pinNumber);
• Digital Input values are only HIGH (On) or LOW (Off)
digitalRead()
if()
statements /
Boolean
Digital Sensors
• Digital sensors are more straight forward than Analog
• No matter what the sensor there are only two settings: On
and Off
• Signal is always either HIGH (On) or LOW (Off)
• Voltage signal for HIGH will be a little less than 5V on your
Uno
• Voltage signal for LOW will be 0V on most systems
[Link]
Programming: Conditional Statements
if()
• void loop()
• {
• int buttonState =
digitalRead(5);
• if(buttonState == LOW) DIG
• { // do something INPUT
• }
• else
• { // do something else
• }
• }
Boolean Operators
<Boolean> Description
( ) == ( ) is equal?
( ) != ( ) is not equal?
( ) > ( ) greater than
( ) >= ( ) greater than or equal
( ) < ( ) less than
( ) <= ( ) less than or equal
Trim pot (Potentiometer)
Variable Resistor
fixed
end
wiper
fixed
end
Programming: Conditional Statements
if()
analogRead()
Serial
communication
Analog Sensors
3 Pin Potentiometer = var. resistor (circuit)
a.k.a. Voltage Divider Circuit
wiper
fixed
ends
1.0 V 1.0 V
analogRead()
• Arduino uses a 10-bit A/D Converter:
• this means that you get input values from 0 to
1023
• 0V0
• 5 V 1023
•Ex:
• int sensorValue = analogRead(A0);
Using Serial Communication
Method used to transfer data between two devices.
Data passes between the computer and Arduino through
the USB cable. Data is transmitted as zeros (‘0’) and ones
(‘1’) sequentially.
Arduino dedicates Digital I/O pin # 0 to receiving
and Digital I/O pin #1 to transmit.
Serial Monitor & analogRead()
Initializes the Serial
Communication
9600 baud data rate
prints data to serial bus
Serial Monitor & analogRead()
Opens up a Serial
Terminal Window
Analog Sensors
2 Pin Analog Sensors = var. resistor
•Take two sensors -- Use the
Serial Monitor and find the
range of input values you get
for each sensor.
•MaxAnalogRead = _________
•MinAnalogRead = _________
Analog Sensors
Examples:
Sensors Variables
Mic soundVolume
Photoresistor lightLevel
Potentiometer dialPosition
Temp Sensor temperature
Flex Sensor bend
Accelerometer tilt/acceleration
Serial
communication
Additional Serial Communication
Sending a Message
void loop ( )
{
[Link](“Hands on “) ;
[Link](“Learning ”) ;
[Link](“is Fun!!!”) ;
}
Serial Communication:
Serial Debugging
void loop()
{
int xVar = 10;
[Link] ( “Variable xVar is “ ) ;
[Link] ( xVar ) ;
}
Blink LED
void setup() {
pinMode (13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
}
Switch LED
int pushbutton = 2;
void setup() {
pinMode(pushbutton, INPUT);
pinMode(13, OUTPUT);
}
void loop() {
int buttonState = digitalRead(pushbutton);
if(buttonState == 1)
{
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
}
UART Transmit & Receive
char s;
void setup() {
[Link](9600);
}
void loop() {
if ([Link]())
{
s=[Link]();
[Link](s);
//[Link]();
}
ADC in UART
int potPin = A0;
int val = 0;
void setup()
{
[Link](9600);
}
void loop()
{
val = analogRead(potPin); // read the value from the sensor
//float voltage = val * (5.0 / 1023.0); // For Convert into Voltage
//float adcrd = (adcrd * 500.0)/1023.0; // For Convert into Temperature
[Link](val); // [Link](voltage); // For print the voltage
delay(500); // delay in between reads for stability
}
Hello World in LCD
/*
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
*/
Hello World in LCD
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
void setup() {
// set up the LCD's number of columns and rows:
[Link](16, 2);
// Print a message to the LCD.
[Link](“Hello World!");
}
Hello World in LCD
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
[Link](5, 1);
// print the number of seconds since reset:
[Link](millis() / 1000);
}
ADC in LCD
/*
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
*/
ADC in LCD
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
unsigned long adcrd;
char ch[6];
void setup() {
// set up the LCD's number of columns and rows:
[Link](16, 2);
// Print a message to the LCD.
[Link](0,0);
[Link]("Pot Example:");
ADC in LCD
[Link](0,1);
[Link]("Volt = ");
}
void loop()
{
adcrd = analogRead(0);
adcrd = (adcrd * 5000) / 1023;
ch[0]=(adcrd/1000)+48;
ch[1]='.';
ch[2]=((adcrd/100)%10)+48;
ch[3]=((adcrd/10)%10)+48;
ch[4]='V';
ch[5]='\0';
[Link](8,1);
[Link](ch);
}
ADC (LM35) in LCD
/*
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
*/
ADC (LM35) in LCD
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
unsigned long adcrd;
char ch[8];
void setup() {
// set up the LCD's number of columns and rows:
[Link](16, 2);
// Print a message to the LCD.
[Link](0,0);
[Link]("CT Example:");
[Link](0,1);
ADC (LM35) in LCD
[Link]("Vol = ");
[Link](9600);
}
void loop()
{
adcrd = analogRead(0);
adcrd = (adcrd * 5000)/10230;
ch[0]=(adcrd/100)+48;
ch[1]='.';
ch[2]=((adcrd/10)%10)+48;
ch[3]=(adcrd%10)+48;
//ch[3]=223;
ch[4]='V';
ch[5]='\0';
ADC (LM35) in LCD
[Link](8,1);
[Link](ch);
float voltage = adcrd * (5.0 / 1023.0);
[Link](voltage);
delay(400);
}
DC Motor with Arduino
void setup() {
pinMode(7, OUTPUT);
pinMode(5, OUTPUT);
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(7, HIGH);
digitalWrite(5, LOW);
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(5, HIGH);
digitalWrite(7, LOW);
digitalWrite(13, LOW);
delay(1000);
}
Stepper Motor with Arduino (Step by Step)
void setup() {
pinMode(7, OUTPUT);
pinMode(5, OUTPUT);
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(7, HIGH);
digitalWrite(5, LOW);
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(5, HIGH);
digitalWrite(7, LOW);
digitalWrite(13, LOW);
delay(1000);
}
Stepper Motor with Arduino (One Revolution)
#include <Stepper.h>
const int stepsPerRevolution = 200; // change this to fit the number of steps
per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
void setup() {
// set the speed at 60 rpm:
[Link](60);
// initialize the serial port:
[Link](9600);
}
Stepper Motor with Arduino (One Revolution)
void loop() {
// step one revolution in one direction:
[Link]("clockwise");
[Link](stepsPerRevolution);
delay(500);
// step one revolution in the other direction:
[Link]("counterclockwise");
[Link](-stepsPerRevolution);
delay(500);
}
Stepper Motor with Arduino (Speed Control)
#include <Stepper.h>
const int stepsPerRevolution = 200; // change this to fit the number of steps
per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
int stepCount = 0; // number of steps the motor has taken
void setup() {
// nothing to do inside the setup
}
Stepper Motor with Arduino (Speed Control)
void loop() {
// read the sensor value:
int sensorReading = analogRead(A0);
// map it to a range from 0 to 100:
int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// set the motor speed:
if (motorSpeed > 0) {
[Link](motorSpeed);
// step 1/100 of a revolution:
[Link](stepsPerRevolution / 100);
}
}
7 Segment with Arduino
#include "SevSeg.h"
SevSeg sevseg; //Instantiate a seven segment controller object
void setup() {
byte numDigits = 4;
byte digitPins[] = {2, 3, 4, 5};
byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13};
[Link](COMMON_CATHODE, numDigits, digitPins, segmentPins);
[Link](90);
}
7 Segment with Arduino
void loop() {
static unsigned long timer = millis();
static int deciSeconds = 0;
if (millis() >= timer) {
deciSeconds++; // 100 milliSeconds is equal to 1 deciSecond
timer += 100;
if (deciSeconds == 10000) { // Reset to 0 after counting for 1000 seconds.
deciSeconds=0;
}
[Link](deciSeconds, 1);
}
[Link](); // Must run repeatedly
}
/// END ///
7 Segment with Arduino
ARD1
AREF
13
PB5/SCK
12
PB4/MISO
RESET 11
~PB3/MOSI/OC2A
10
~ PB2/SS/OC1B
9
~ PB1/OC1A
8
PB0/ICP1/CLKO
ATMEGA328P-PU
1121
DIGITAL (~PWM)
7
ANALOG IN
PD7/AIN1
6
A0 ~ PD6/AIN0
PC0/ADC0 5
A1 ~ PD5/T1
PC1/ADC1 4
A2 PD4/T0/XCK
PC2/ADC2 3
A3 ~ PD3/INT1
PC3/ADC3 2
A4 PD2/INT0
PC4/ADC4/SDA 1
A5 TX PD1/TXD
PC5/ADC5/SCL 0
RX PD0/RXD
ARDUINO UNO R3