0% found this document useful (0 votes)
71 views

Basic Examples

The document provides code examples to blink an LED, read a digital switch, and read an analog potentiometer. It includes the code, circuit diagrams, and explanations of how each example works by controlling an output, reading a digital input, and reading an analog input respectively.

Uploaded by

EdnaBelegal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

Basic Examples

The document provides code examples to blink an LED, read a digital switch, and read an analog potentiometer. It includes the code, circuit diagrams, and explanations of how each example works by controlling an output, reading a digital input, and reading an analog input respectively.

Uploaded by

EdnaBelegal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 13

BASIC

Blink
This example shows the simplest thing you can do with an Arduino to see physical output: it
blinks an LED.
Hardware Required
Arduino Board
LED
Circuit
To build the circuit, attach a !"ohm resistor to pin #$. Then attach the long leg o% an LED &the
positi'e leg, called the anode( to the resistor. Attach the short leg &the negati'e leg, called the
cathode( to ground. Then plug your Arduino board into your computer, start the Arduino
program, and enter the code below.
)ost Arduino boards already ha'e an LED attached to pin #$ on the board itsel%. *% you run this
example with no hardware attached, you should see that LED blink.
Code
*n the program below, the %irst thing you do is to initiali+e pin #$ as an output pin with the line
pinMode(13, OUTPUT);
*n the main loop, you turn the LED on with the line:
digitalWrite(13, HIGH);
This supplies , 'olts to pin #$. That creates a 'oltage di%%erence across the pins o% the LED, and
lights it up. Then you turn it o%% with the line:
digitalWrite(13, LOW);
That takes pin #$ back to ! 'olts, and turns the LED o%%. *n between the on and the o%%, you want
enough time %or a person to see the change, so the delay() commands tell the Arduino to do
Arduino Program and Circuit Diagram Page 1
nothing %or #!!! milliseconds, or one second. -hen you use the delay() command, nothing
else happens %or that amount o% time. .nce you/'e understood the basic examples, check out the
Blink-ithoutDelay example to learn how to create a delay while doing other things.
.nce you/'e understood this example, check out the Digital0ead1erial example to learn how
read a switch connected to the Arduino.
23
Blink
Turns on an LED on %or one second, then o%% %or one second, repeatedly.

This example code is in the public domain.
32

22 4in #$ has an LED connected on most Arduino boards.
22 gi'e it a name:
int led 5 #$6
22 the setup routine runs once when you press reset:
'oid setup&( 7
22 initiali+e the digital pin as an output.
pin)ode&led, .8T48T(6
9
22 the loop routine runs o'er and o'er again %ore'er:
'oid loop&( 7
digital-rite&led, :*;:(6 22 turn the LED on &:*;: is the 'oltage le'el(
delay&#!!!(6 22 wait %or a second
digital-rite&led, L.-(6 22 turn the LED o%% by making the 'oltage L.-
delay&#!!!(6 22 wait %or a second
Digital Read Serial
This example shows you how to monitor the state o% a switch by establishing serial
communication between your Arduino and your computer o'er 81B.
Hardware Required
Arduino Board
A momentary switch, button, or toggle switch
#!k ohm resistor
breadboard
hook"up wire
Arduino Program and Circuit Diagram Page 2
<onnect three wires to the Arduino board. The %irst two, red and black, connect to the two long
'ertical rows on the side o% the breadboard to pro'ide access to the , 'olt supply and ground. The
third wire goes %rom digital pin to one leg o% the pushbutton. That same leg o% the button
connects through a pull"down resistor &here #! =.hms( to ground. The other leg o% the button
connects to the , 'olt supply.
4ushbuttons or switches connect two points in a circuit when you press them. -hen the
pushbutton is open &unpressed( there is no connection between the two legs o% the pushbutton, so
the pin is connected to ground &through the pull"down resistor( and reads as L.-, or !. -hen
the button is closed &pressed(, it makes a connection between its two legs, connecting the pin to ,
'olts, so that the pin reads as :*;:, or #.
*% you disconnect the digital i2o pin %rom e'erything, the LED may blink erratically. This is
because the input is >%loating> " that is, it doesn/t ha'e a solid connection to 'oltage or ground,
and it will randomly return either :*;: or L.-. That/s why you need a pull"down resistor in
the circuit.
Code
*n the program below, the 'ery %irst thing that you do will in the setup %unction is to begin serial
communications, at ?@!! bits o% data per second, between your Arduino and your computer with
the line:
Serial.egin(!"##);
Aext, initiali+e digital pin , the pin that will read the output %rom your button, as an input:
pinMode($,I%PUT);
Aow that your setup has been completed, mo'e into the main loop o% your code. -hen your
button is pressed, , 'olts will %reely %low through your circuit, and when it is not pressed, the
input pin will be connected to ground through the #!"kilohm resistor. This is a digital input,
meaning that the switch can only be in either an on state &seen by your Arduino as a >#>, or
:*;:( or an o%% state &seen by your Arduino as a >!>, or L.-(, with nothing in between.
The %irst thing you need to do in the main loop o% your program is to establish a 'ariable to hold
the in%ormation coming in %rom your switch. 1ince the in%ormation coming in %rom the switch
will be either a >#> or a >!>, you can use an int datatype. <all this 'ariable &en&or'al(e, and
set it to eBual whate'er is being read on digital pin . Cou can accomplish all this with Dust one
line o% code:
Arduino Program and Circuit Diagram Page 3
int &en&or'al(e ) digital*ead($);
.nce the Arduino has read the input, make it print this in%ormation back to the computer as a
decimal 'alue. Cou can do this with the command 1erial.println&( in our last line o% code:
Serial.println(&en&or'al(e);
Aow, when you open your 1erial )onitor in the Arduino en'ironment, you will see a stream o%
>!>s i% your switch is open, or >#>s i% your switch is closed.
23
Digital0ead1erial
0eads a digital input on pin , prints the result to the serial monitor

This example code is in the public domain.
32
22 digital pin has a pushbutton attached to it. ;i'e it a name:
int pushButton 5 6
22 the setup routine runs once when you press reset:
'oid setup&( 7
22 initiali+e serial communication at ?@!! bits per second:
1erial.begin&?@!!(6
22 make the pushbutton/s pin an input:
pin)ode&pushButton, *A48T(6
9
22 the loop routine runs o'er and o'er again %ore'er:
'oid loop&( 7
22 read the input pin:
int button1tate 5 digital0ead&pushButton(6
22 print out the state o% the button:
1erial.println&button1tate(6
delay&#(6 22 delay in between reads %or stability
9
Analog Read Serial
This example shows you how to read analog input %rom the physical world using a
potentiometer. A potentiometer is a simple mechanical de'ice that pro'ides a 'arying amount o%
resistance when its sha%t is turned. By passing 'oltage through a potentiometer and into an
analog input on your Arduino, it is possible to measure the amount o% resistance produced by a
potentiometer &or pot %or short( as an analog 'alue. *n this example you will monitor the state o%
your potentiometer a%ter establishing serial communication between your Arduino and your
computer.
Hardware Required
Arduino Board
#!"kilohm 4otentiometer
Circuit
Arduino Program and Circuit Diagram Page 4
<onnect the three wires %rom the potentiometer to your Arduino board. The %irst goes to ground
%rom one o% the outer pins o% the potentiometer. The second goes %rom , 'olts to the other outer
pin o% the potentiometer. The third goes %rom analog input to the middle pin o% the
potentiometer.
By turning the sha%t o% the potentiometer, you change the amount o% resistance on either side o%
the wiper which is connected to the center pin o% the potentiometer. This changes the 'oltage at
the center pin. -hen the resistance between the center and the side connected to , 'olts is close
to +ero &and the resistance on the other side is close to #! kilohms(, the 'oltage at the center pin
nears , 'olts. -hen the resistances are re'ersed, the 'oltage at the center pin nears ! 'olts, or
ground. This 'oltage is the analog voltage that you/re reading as an input.
The Arduino has a circuit inside called an analog-to-digital converter that reads this changing
'oltage and con'erts it to a number between ! and #!$. -hen the sha%t is turned all the way in
one direction, there are ! 'olts going to the pin, and the input 'alue is !. -hen the sha%t is turned
all the way in the opposite direction, there are , 'olts going to the pin and the input 'alue is
#!$. *n between, analog0ead&( returns a number between ! and #!$ that is proportional to the
amount o% 'oltage being applied to the pin.
Code
*n the program below, the only thing that you do will in the setup %unction is to begin serial
communications, at ?@!! bits o% data per second, between your Arduino and your computer with
the command:
Serial.egin(!"##);
Aext, in the main loop o% your code, you need to establish a 'ariable to store the resistance 'alue
&which will be between ! and #!$, per%ect %or an int datatype( coming in %rom your
potentiometer:
int &en&or'al(e ) analog*ead(+#);
Einally, you need to print this in%ormation to your serial window as a decimal &,-.( 'alue. Cou
can do this with the command 1erial.println&( in your last line o% code:
Arduino Program and Circuit Diagram Page 5
Serial.println(&en&or'al(e, ,-.)
Aow, when you open your 1erial )onitor in the Arduino de'elopment en'ironment &by clicking
the button directly to the right o% the >8pload> button in the header o% the program(, you should
see a steady stream o% numbers ranging %rom !"#!$, correlating to the position o% the pot. As
you turn your potentiometer, these numbers will respond almost instantly.
23
Analog0ead1erial
0eads an analog input on pin !, prints the result to the serial monitor.
Attach the center pin o% a potentiometer to pin A!, and the outside pins to F,G and ground.

This example code is in the public domain.
32
22 the setup routine runs once when you press reset:
'oid setup&( 7
22 initiali+e serial communication at ?@!! bits per second:
1erial.begin&?@!!(6
9
22 the loop routine runs o'er and o'er again %ore'er:
'oid loop&( 7
22 read the input on analog pin !:
int sensorGalue 5 analog0ead&A!(6
22 print out the 'alue you read:
1erial.println&sensorGalue(6
delay&#(6 22 delay in between reads %or stability
9
Fading
Demonstrates the use o% the analog-rite&( %unction in %ading an LED o%% and on. Analog-rite
uses pulse width modulation &4-)(, turning a digital pin on and o%% 'ery Buickly, to create a
%ading e%%ect.
Hardware Required
Arduino board
Breadboard
a LED
a ! ohm resistor
Circuit
<onnect the anode &the longer, positi'e leg( o% your LED to digital output pin ? on your Arduino
through a !"ohm resistor. <onnect the cathode &the shorter, negati'e leg( directly to ground.
Arduino Program and Circuit Diagram Page 6
Code
A%ter declaring pin ? to be your ledPin, there is nothing to do in the &et(p() %unction o% your
code.
The analogWrite() %unction that you will be using in the main loop o% your code reBuires two
arguments: .ne telling the %unction which pin to write to, and one indicating what 4-) 'alue to
write.
*n order to %ade your LED o%% and on, gradually increase your 4-) 'alue %rom ! &all the way
o%%( to ,, &all the way on(, and then back to ! once again to complete the cycle. *n the sketch
below, the 4-) 'alue is set using a 'ariable called rig/tne&&. Each time through the loop, it
increases by the 'alue o% the 'ariable 0ade+1o(nt.
*% rig/tne&& is at either extreme o% its 'alue &either ! or ,,(, then 0ade+1o(nt is changed to
its negati'e. *n other words, i% 0ade+1o(nt is ,, then it is set to ",. *% it/s ,,, then it/s set to ,. The
next time through the loop, this change causes rig/tne&& to change direction as well.
analogWrite() can change the 4-) 'alue 'ery %ast, so the delay at the end o% the sketch
controls the speed o% the %ade. Try changing the 'alue o% the delay and see how it changes the
program.
23
Eade

This example shows how to %ade an LED on pin ?
using the analog-rite&( %unction.

This example code is in the public domain.
32
int led 5 ?6 22 the pin that the LED is attached to
int brightness 5 !6 22 how bright the LED is
int %adeAmount 5 ,6 22 how many points to %ade the LED by
22 the setup routine runs once when you press reset:
'oid setup&( 7
22 declare pin ? to be an output:
pin)ode&led, .8T48T(6
9
Arduino Program and Circuit Diagram Page 7
22 the loop routine runs o'er and o'er again %ore'er:
'oid loop&( 7
22 set the brightness o% pin ?:
analog-rite&led, brightness(6
22 change the brightness %or next time through the loop:
brightness 5 brightness F %adeAmount6
22 re'erse the direction o% the %ading at the ends o% the %ade:
i% &brightness 55 ! HH brightness 55 ,,( 7
%adeAmount 5 "%adeAmount 6
9
22 wait %or $! milliseconds to see the dimming e%%ect
delay&$!(6
9
Analog Read Voltage
This example shows you how to read an analog input on 4in !, con'ert the 'alues %rom
analog0ead&( into 'oltage, and print it out to the serial monitor.
Hardware Required
Arduino Board
a 'ariable resistor, like a potentiometer
Arduino Program and Circuit Diagram Page 8
<onnect the three wires %rom the potentiometer to your Arduino board. The %irst goes to ground
%rom one o% the outer pins o% the potentiometer. The second goes %rom , 'olts to the other outer
pin o% the potentiometer. The third goes %rom analog input to the middle pin o% the
potentiometer.
By turning the sha%t o% the potentiometer, you change the amount o% resistance on either side o%
the wiper which is connected to the center pin o% the potentiometer. This changes the 'oltage at
the center pin. -hen the resistance between the center and the side connected to , 'olts is close
to +ero &and the resistance on the other side is close to #! kilohms(, the 'oltage at the center pin
nears , 'olts. -hen the resistances are re'ersed, the 'oltage at the center pin nears ! 'olts, or
ground. This 'oltage is the analog voltage that you/re reading as an input.
The Arduino has a circuit inside called an analog-to-digital converter that reads this changing
'oltage and con'erts it to a number between ! and #!$. -hen the sha%t is turned all the way in
one direction, there are ! 'olts going to the pin, and the input 'alue is !. -hen the sha%t is turned
all the way in the opposite direction, there are , 'olts going to the pin and the input 'alue is
#!$. *n between, analog0ead&( returns a number between ! and #!$ that is proportional to the
amount o% 'oltage being applied to the pin.
Code
*n the program below, the 'ery %irst thing that you do will in the setup %unction is to begin serial
communications, at ?@!! bits o% data per second, between your Arduino and your computer with
the line:
Serial.egin(!"##);
Aext, in the main loop o% your code, you need to establish a 'ariable to store the resistance 'alue
&which will be between ! and #!$, per%ect %or an int datatype( coming in %rom your
potentiometer:
int &en&or'al(e ) analog*ead(+#);
To change the 'alues %rom !"#!$ to a range that corresponds to the 'oltage the pin is reading,
you/ll need to create another 'ariable, a 0loat, and do a little math. To scale the numbers
between !.! and ,.!, di'ide ,.! by #!$.! and multiply that by sensorValue :
0loat 2oltage) &en&or'al(e 3 (4.# 5 1#$3.#);
Einally, you need to print this in%ormation to your serial window as. Cou can do this with the
command 1erial.println&( in your last line o% code:
Serial.println(2oltage)
Aow, when you open your 1erial )onitor in the Arduino de'elopment en'ironment &by clicking
the button directly to the right o% the >8pload> button in the header o% the program(, you should
see a steady stream o% numbers ranging %rom !.! " ,.!. As you turn the pot, the 'alues will
change, corresponding to the 'oltage coming into pin A!.
23
0eadAnalogGoltage
0eads an analog input on pin !, con'erts it to 'oltage, and prints the result to the serial monitor.
Attach the center pin o% a potentiometer to pin A!, and the outside pins to F,G and ground.

This example code is in the public domain.
32
Arduino Program and Circuit Diagram Page 9
22 the setup routine runs once when you press reset:
'oid setup&( 7
22 initiali+e serial communication at ?@!! bits per second:
1erial.begin&?@!!(6
9
22 the loop routine runs o'er and o'er again %ore'er:
'oid loop&( 7
22 read the input on analog pin !:
int sensorGalue 5 analog0ead&A!(6
22 <on'ert the analog reading &which goes %rom ! " #!$( to a 'oltage &! " ,G(:
%loat 'oltage 5 sensorGalue 3 &,.! 2 #!$.!(6
22 print out the 'alue you read:
1erial.println&'oltage(6
9
Digital
Blink ithout Dela!
1ometimes you need to do two things at once. Eor example you might want to blink an LED &or
some other time"sensiti'e %unction( while reading a button press or other input. *n this case, you
can/t use delay(), or you/d stop e'erything else the program while the LED blinked. The
program might miss the button press i% it happens during the delay&(. This sketch demonstrates
how to blink the LED without using delay(). *t keeps track o% the last time the Arduino turned
the LED on or o%%. Then, each time through loop(), it checks i% a long enough inter'al has
passed. *% it has, it toggles the LED on or o%%.
Hardware Required
Arduino Board
LED
To uild t!e circuit" gra an #$D and attac! it% long" &o%iti'e leg (called t!e anode)
to &in 13* Attac! t!e %!ort" negati'e leg (called t!e anode) to ground* T!en &lug
Arduino Program and Circuit Diagram Page 1+
,our Arduino oard into ,our com&uter" %tart t!e Arduino &rogram" and enter t!e
code elo-*
Code
The code below uses the 1illi&() %unction, a command that returns the number o% milliseconds
since the Arduino board started running its current program, to blink an LED.
23 Blink without Delay

Turns on and o%% a light emitting diode&LED( connected to a digital
pin, without using the delay&( %unction. This means that other code
can run at the same time without being interrupted by the LED code.

The circuit:
3 LED attached %rom pin #$ to ground.
3 Aote: on most Arduinos, there is already an LED on the board
that/s attached to pin #$, so no hardware is needed %or this example.


22 constants won/t change. 8sed here to
22 set pin numbers:
const int led4in 5 #$6 22 the number o% the LED pin
22 Gariables will change:
int led1tate 5 L.-6 22 led1tate used to set the LED
long pre'ious)illis 5 !6 22 will store last time LED was updated
22 the %ollow 'ariables is a long because the time, measured in miliseconds,
22 will Buickly become a bigger number than can be stored in an int.
long inter'al 5 #!!!6 22 inter'al at which to blink &milliseconds(
'oid setup&( 7
22 set the digital pin as output:
pin)ode&led4in, .8T48T(6
9
'oid loop&(
7
22 here is where you/d put code that needs to be running all the time.
22 check to see i% it/s time to blink the LED6 that is, i% the
22 di%%erence between the current time and last time you blinked
22 the LED is bigger than the inter'al at which you want to
22 blink the LED.
unsigned long current)illis 5 millis&(6

i%&current)illis " pre'ious)illis I inter'al( 7
22 sa'e the last time you blinked the LED
pre'ious)illis 5 current)illis6
22 i% the LED is o%% turn it on and 'ice"'ersa:
i% &led1tate 55 L.-(
led1tate 5 :*;:6
else
led1tate 5 L.-6
Arduino Program and Circuit Diagram Page 11
22 set the LED with the led1tate o% the 'ariable:
digital-rite&led4in, led1tate(6
9
9
Button
4ushbuttons or switches connect two points in a circuit when you press them. This example turns
on the built"in LED on pin #$ when you press the button.
Hardware
Arduino Board
momentary button or switch
#!= ohm resistor
breadboard
hook"up wire
<onnect three wires to the Arduino board. The %irst two, red and black, connect to the two long
'ertical rows on the side o% the breadboard to pro'ide access to the , 'olt supply and ground. The
third wire goes %rom digital pin to one leg o% the pushbutton. That same leg o% the button
connects through a pull"down resistor &here #! =.hms( to ground. The other leg o% the button
connects to the , 'olt supply.
-hen the pushbutton is open &unpressed( there is no connection between the two legs o% the
pushbutton, so the pin is connected to ground &through the pull"down resistor( and we read a
L.-. -hen the button is closed &pressed(, it makes a connection between its two legs,
connecting the pin to , 'olts, so that we read a :*;:.
Cou can also wire this circuit the opposite way, with a pullup resistor keeping the input :*;:,
and going L.- when the button is pressed. *% so, the beha'ior o% the sketch will be re'ersed,
with the LED normally on and turning o%% when you press the button.
*% you disconnect the digital i2o pin %rom e'erything, the LED may blink erratically. This is
because the input is >%loating> " that is, it will randomly return either :*;: or L.-. That/s why
you need a pull"up or pull"down resistor in the circuit.
Arduino Program and Circuit Diagram Page 12
Code
23
Button

Turns on and o%% a light emitting diode&LED( connected to digital
pin #$, when pressing a pushbutton attached to pin .


The circuit:
3 LED attached %rom pin #$ to ground
3 pushbutton attached to pin %rom F,G
3 #!= resistor attached to pin %rom ground

3 Aote: on most Arduinos there is already an LED on the board
attached to pin #$.


22 constants won/t change. They/re used here to
22 set pin numbers:
const int button4in 5 6 22 the number o% the pushbutton pin
const int led4in 5 #$6 22 the number o% the LED pin
22 'ariables will change:
int button1tate 5 !6 22 'ariable %or reading the pushbutton status
'oid setup&( 7
22 initiali+e the LED pin as an output:
pin)ode&led4in, .8T48T(6
22 initiali+e the pushbutton pin as an input:
pin)ode&button4in, *A48T(6
9
'oid loop&(7
22 read the state o% the pushbutton 'alue:
button1tate 5 digital0ead&button4in(6
22 check i% the pushbutton is pressed.
22 i% it is, the button1tate is :*;::
i% &button1tate 55 :*;:( 7
22 turn LED on:
digital-rite&led4in, :*;:(6
9
else 7
22 turn LED o%%:
digital-rite&led4in, L.-(6
9
9
Arduino Program and Circuit Diagram Page 13

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy