Arduino
Arduino
Plug in the Arduino board with the USB cable to the computer. The
"ON" LED lights up (in green or orange).
void setup()
setup(): configures pin modes {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
}
• pinMode takes two arguments, the number of the Arduino pin and
whether it is specified as INPUT or OUTPUT. For example, if we
connect an LED to digital I/O pin 9:
void setup() {
}
Programming the Arduino
• loop(): runs the main body of the program forever until the power
is shut off or someone presses the RESET button. This is where
important things in your program happen.
Variables
• A variable is a place for storing a piece of data. It has a name, a
type, and a value.
Functions
• A function is a named piece of code that can be used from
elsewhere in a sketch. A function that is already been defined can
be called by stating the function name and passing its parameters.
Arduino Programming
Essential Functions
digitalWrite(9, LOW);
digitalRead reads the value from a specified digital pin that has
been configured as an input (either HIGH or LOW).
digitalRead(8);
If the switch is OFF, the function will return LOW, else it returns
HIGH.
Programming the Arduino
• Another interesting function is the delay function which pauses the
program for the amount of time (in miliseconds) specified as
parameter.
• Fortunately, the voltage across and current through the LED can
be reduced by using a resistor (What value)?
• If you are not sure, select a slightly higher value resistor because it
is better to have a dim LED than a dead one!
Flashing LED
Write a sketch that flashes an LED connected to Pin 2 continuously
(on/off for 0.5 second each)
The benefit of using a for loop is that you can determine how many
times the code inside the loop will repeat.
Logical Operators
if (!a)
Output Analog Signals
• With digitalWrite function, only digital signals can be output at the
I/O pins (LOW or HIGH).
• Often, in real circuits, you need to adjust the output within a range.
For example:
Brightness of a lamp
a volume knob on a stereo
a heat setting on an oven
a steering wheel in a car
speed of a DC motor
• The Arduino board (like other digital devices) does not have
analog output. Instead, it uses Pulse Width Modulation (PWM) to
output analog signals. The Arduino Uno has 6 digital I/O pins that
can be used as PWM (pins 3, 5, 6, 9, 10 and 11, marked ~)
Pulse Width Modulation (PWM)
A microcontroller can easily work with inputs and outputs that have
only two states: on and off (for example, LED, relay).
Sometimes more than just "on" & "off " control of device is needed.
For example, control the brightness of a lamp or the speed of DC
motor (signals between 0 and 5V). This situation can be handled by a
technique called PWM.
Generate a square wave and control the % of total time the output is
high. Also ensure that frequency is high enough.
Pulse Width Modulation (PWM)
• analogWrite(pin, duty-cycle) is used to output a PWM signal
where pin is the digital pin from which PWM signal is output and
Example:
Write a sketch that will turn on an LED connected to I/O pin 3 and
progressively increase its level of brightness until it is fully lit. Then,
the sketch decreases the brightness of the LED gradually until it is off.
Example
#define LED 3
int d = 5;
void setup()
{
pinMode(LED, OUTPUT);
}
for ( int a = 255 ; a >= 0 ; a-- )
void loop()
{
{
analogWrite(LED, a);
for ( int a = 0 ; a < 256 ; a++ )
delay(d);
{
}
analogWrite(LED, a);
delay(d);
delay(1000);
}
}
Reading Analog Inputs
• As mentioned earlier, there are six analog input pins on the
Arduino UNO that allows measurement of analog voltage values.
• Since the Arduino processes only 0s and 1s, it will map the input
voltage (between 0-5 V) into integer values between 0 and 1023.
This yields a resolution of: 5 V/1024 units or 4.9 mV per unit.
Reading Analog Inputs
• Usually in sketches, an integer variable is used to store the value
returned by the analogRead() function.
int a = analogRead(2);
IMPORTANT NOTE
Either divide by 4
Or use map(value, fromLow, fromHigh, toLow, toHigh)
function.
Reading Analog Inputs
• How to make an analog input signal (varying input signal)?
Hardware Required:
1x Arduino Uno
1x USB cable
1x Breadboard
1x LED
1x Potentiometer
1x 220 Ohm resistor (Red Red Brown Gold)
7x Jumper Wires
Example
int pot_pin = 0;
int led_pin = 11;
//variable for storing potentiometer value
int pot_value = 0;
void setup()
{
pinMode(led_pin, OUTPUT);
}
void loop()
{
pot_value = analogRead(pot_pin); // Read potentiometer value
analogWrite(led_pin, pot_value / 4); // map value and output to LED
delay(50);
}
The Serial Monitor
• The Arduino IDE has a window called
the Serial Monitor which can be
opened by clicking on the upper-right
icon.
• It allows you to both send messages
from your computer to an Arduino
board (over USB) and also to receive
messages from the Arduino.
• It is very useful during debugging as it
allows you to see what’s going on as
the program executes.
• In particular, it allows you to print
input values and messages to a serial
monitor.
The Serial Monitor
• To establish communication
between your Arduino board
and a laptop/ computer,
include Serial.begin(9600)
statement in setup()
• You can then use the Serial.print() to send information from your
Arduino to your computer, so you can see the value or message
displayed on your computer’s monitor in real-time.