Arduino Programming
I/O Pins
Terminology
sketch - a program you write to run on an Arduino board
pin - an input or output connected to something. e.g.
output to an LED, input from a knob.
digital value - is either HIGH or LOW. (aka on/off,
one/zero) e.g. switch state
analog - value ranges, usually from 0-255. e.g. LED
brightness, motor speed, etc.
setup
Concepts: INPUT vs. OUTPUT
The setup section is used for assigning input and outputs
Inputs - is a signal / information going into the board.
o (Examples: motors, LED’s, sensors etc) to ports
o Examples: Buttons Switches, Light Sensors,
on the Arduino
Flex Sensors, Humidity Sensors, Temperature
It also specifies whether the device is OUTPUT or INPUT
Sensors
To do this we use the command “pinMode”
Output - is any signal exiting the board
o Examples: LEDs, DC motor, servo motor, a
piezo buzzer, relay, an RGB LED
Microcontrollers are digital devices – ON or OFF.
Also called – discrete.
Analog signals are anything that can be a full range of
values.
Digital I/0
pinMode(pin, mode)
o Sets pin to either INPUT or OUTPUT
digitalRead(pin)
o Reads HIGH or LOW from a pin
digitalWrite(pin, value)
o Writes HIGH or LOW to a pin
Electronic stuff pinMode
Output pins can provide 40 mA of current
Writing HIGH to an input pin installs a 20KΩ pullup A pin on arduino can be set as input or output by using
pinModefunction.
Arduino Timing pinMode(13, OUTPUT); // sets pin 13 as output pin
pinMode(13, INPUT); // sets pin 13 as input pin
delay(ms) – Pauses for a few milliseconds
delayMicroseconds(us) – Pauses for a few microseconds
o int buttonState = digitalRead (pinNumber);
Digital Input values are only HIGH (On) or LOW (Off)
Digital Sensors
Reading/writing digital values
Digital sensors are more straight forward than Analog
digitalWrite(13, LOW); // Makes the output voltage on No matter what the sensor there are only two settings:
pin 13 , 0V On and Off
digitalWrite(13, HIGH); // Makes the output voltage on Signal is always either HIGH (On) or LOW (Off)
pin 13 , 5V Voltage signal for HIGH will be a little less than 5V on
int buttonState = digitalRead(2); // reads the value of pin your Uno
2 in buttonState Voltage signal for LOW will be 0V on most systems
Variables
A variable is like “bucket”
It holds numbers or other values temporarily
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: ANALOG I/O
o pinMode (pinNumber, INPUT);
o Make sure to use ALL CAPS for INPUT Analog Output
To get a digital reading:
Can a digital devise produce analog output?
Analog output can be simulated using pulse width value is duty cycle: between 0 and 255
modulation (PWM) Examples:
analogWrite(9, 128)
o for a 50% duty cycle
analogWrite(11, 64)
o for a 25% duty cycle
Pulse Width Modulation
Can’t use digital
pins to directly
supply say 2.5V,
but can pulse
the output on
and off really
fast to produce
the same effect
The on-off
pulsing happens
Analog Input
so quickly, the
connected
Think about music stored on a CD---an analog signal
output device
captured on digital media
“sees” the result
o Sample rate
as a reduction in
o Word length
the voltage
Arduino Analog Input
Resolution: the number of different voltage levels (i.e.,
states) used to discretize an input signal
Resolution values range from 256 states (8 bits) to
4,294,967,296 states (32 bits)
The Arduino uses 1024 states (10 bits)
Smallest measurable voltage change is 5V/1024 or 4.8
mV
Maximum sample rate is 10,000 times a second
PMW Pins
Command: analogWrite(pin,value)
How does ADC work?
Serial Communication
Serial Communication
Compiling turns your program into binary data (ones
and zeros)
Uploading sends the
bits through USB
cable to the Arduino
The two LEDs near
the USB connector
blink when data is
transmitted
RX blinks when the
Arduino is receiving data
TX blinks when the Arduino is transmitting data
Some Commands
Serial.begin() - e.g., Serial.begin(9600)
Serial.print() or Serial.println() - e.g., Serial.print(value)
Serial.read()
Serial.available()
Serial.write()
Serial.parseInt()