Arduino Lesson 9
Analog readings
Intro
We start to learn how to use the analog I/O [Link]
are 6 analog interface(#0-#5).These interfaces can be used
as digital interfaces(#14-#19). Potentiometer is a widely
use
analog output electronic [Link] we use it to accomplish
this
experiment.
Parts required
Potentiometer x 1
Breadboard x 1
Jumper wires x 1 bundle
Connect-it-up
Here we use analog interface #0.
Experiment Intro.
We will turn the resistance of the potentiometer into
analog and read it,display on the screen at [Link]
experiment contents the functions you need to use in the
future experiments.
The program is simple. One function,anlogRead() sentence
can
Read the analog value. Arduino 328 is a 10-bit A/D
gather,so
The reading range from 0-1023. The difficulty of this
experiment is putting the values on screen.
First we need to setup baud rate in voidsetup(),the values
means
Commnunications between Arduino and PC,so the baud rate
need
To be same, otherwise there would be just messy code or
nothing
[Link] is a baud rate setup button on right bottom
corner of the besel
Window of [Link] baud rate here should be same as
functon void setup() in program.
The sentence to set up baud rate is [Link]();baud rate
in the brackets,then it is the sentence display the values,
[Link](); or [Link](); both are
ok,difference is the latter automactically
return.
Program for reference
int potpin=0;//Define analog interface #0
int ledpin=13;//Define digital interface #13
int val=0;//Define variable val and assign it to 0
void setup()
pinMode(ledpin,OUTPUT);//Definte digital interface as output
[Link](9600);//set the baud rate as 9600
void loop()
digitalWrite(ledpin,HIGH);//light up led on digital interface
#13
delay(50);//delay 0.05 s
digitalWrite(ledpin,LOW);//Turn off #13 LED
delay(50);//delay 0.05 s
val=analogRead(potpin);//read value from analog interface #0
and assign it to val
[Link](val);//display value of val
}
This program use #13(LED light) digital interface of
Arduino,the led will flash every time it got the reading.
Below is the readings.
Done! You can see the data on the screen is changing while
you twist the potentiometer.