You are on page 1of 7

University of Engineering and Technology, Lahore Spring 2018

ARDUINO 2: READING ANALOG INPUTS AND SENDING


SERIAL DATA
Name : Date :
Regd-No :
OBJECTIVES:
▪ Use the analogRead() function to read analog signals.
▪ Send the data to Arduino ‘serial monitor’ using serial library.
SUGGESTED READING:
▪ http://arduino.cc/en/Guide/HomePage
▪ http://learn.adafruit.com/adafruit-arduino-lesson-5-the-serial-monitor
▪ http://learn.adafruit.com/adafruit-arduino-lesson-8-analog-inputs
▪ http://arduino.cc/en/Reference/analogRead
▪ http://www.ladyada.net/learn/arduino/lesson4.html
▪ http://playground.arduino.cc/uploads/Documentation/Arduino_programming_notebook.p
df

Please read through all the suggested reading before you come to lab.

EQUIPMENT AND COMPONENTS:


▪ Arduino™ UNO or similar Board
▪ Standard A-B type USB cable
▪ Variable Resistor
▪ LEDs
▪ Jumper wires
▪ Push Buttons
---------------------------------------------------------------------------------------------------------------------
Reading Analog Values using analogRead():
The analogRead() function is used to read analog voltages on the analog pins
of the Arduino using the on board analog-to-digital converter. The Arduino board
contains a 6 channel (8 channels on the Mini and Nano, 16 on the Mega), 10-bit
analog to digital converter. This means that it will map input voltages between 0
and 5 volts into integer values between 0 and 1023. This yields a resolution
between readings of:

5 volts / 1024 units


or,
.0049 volts (4.9 mV) per unit.

MCT-222: EMBEDDED SYSTEMS


1
ARD 2
University of Engineering and Technology, Lahore Spring 2018

The input range and resolution can be changed using analogReference().It takes
about 100 microseconds (0.0001 s) to read an analog input, so the maximum
reading rate is about 10,000 times a second.

Syntax:
analogRead(pin)

Parameters:
pin: the number of the analog input pin to read from (0 to 5 on most boards,
0 to 7 on the Mini and Nano, 0 to 15 on the Mega)

Returns
int (0 to 1023)

Serial Communication using Serial Library:


In lab lecture no 2, we studied what functions are, and how to use functions
to control inputs and outputs (pinMode(), analogRead() etc). A function is
essentially a piece of code that performs a specific task. It can be user defined or a
built-in function, like the analogRead() function that reads an analog input from a
pin.
A software library is collection of functions, where all the functions are
similar in nature (related to performing the same task). The Arduino IDE comes
with quite a lot of libraries for ease of programming, and the user has the option to
create his own code libraries.
The library we will be using is the Serial
Library, which allows the Arduino to send data
back to the computer (Fig.2.1). The word serial
means "one after the other". Serial data transfer is
when we transfer data one bit at a time, one right
after the other. Information is passed back & forth
between the computer and Arduino by, essentially,
setting a pin high or low. Just like we used that
technique to turn an LED on and off, we can also
send data. One side sets the pin and the other reads Fig.2.1: Serial Library and its Functions
it. It's a little like Morse code, where you can use

MCT-222: EMBEDDED SYSTEMS


2
ARD 2
University of Engineering and Technology, Lahore Spring 2018
‘dits’ and ‘dahs’ to send messages by telegram. In this case, instead of a long
cable, its only a few feet (Fig.5.2)

Fig.2.2: Communication between Arduino and Computer

Example 2_1:
/*
* Hello World!
*
* This is the Hello World! for Arduino.
* It shows how to send data to the computer
*/

void setup() // run once, when the sketch starts


{
Serial.begin(9600); // set up Serial library at 9600 bps
}

void loop() // run over and over again


{
Serial.println("Hello world!"); // prints hello with ending line break
delay(1000);
}

Upload the Example code to the Arduino, and turn on the serial monitor by
clicking on the button (Fig.2.3).

MCT-222: EMBEDDED SYSTEMS


3
ARD 2
University of Engineering and Technology, Lahore Spring 2018

Fig.2.3: Using the Serial Monitor

Notice the new window popup!? It contains the text message you transmitted from the Arduino.
Notice the TX led on the board light up when the communication occurs. It shows that the
Arduino is transmitting some digital data through the USB cable to the computer. You might
have also noticed the RX led blink when you upload your sketch to the Arduino. That means the
computer is sending some digital information to the board (information about the sketch). Let’s
go over some functions related to serial library to see what’s going on. When using a function
from a library, we must point out the compiler about the library we intend to use. So we must
write the name of the library before the actual function name (shows which library does the
function belong to)

Serial.begin(baud_rate);
This begin() function initializes the communication from the microcontroller. The
amount given as input argument is the speed of communication. As we’ll learn later, it should be
understood by the sender and the receiver at what speed the data is being transmitted. 9600
means the communication is going on at 9600 bits per second (well, not exactly, but the idea is
similar).

Serial.println("Hello world!");
The println() function sends the text or variable data to the computer, and moves the
cursor to next line (the next data begins from a new line)

Serial.print("Hello world!");
The print() finction sends the text data to computer, but lets the cursor stay there, so the
next data starts from where the previous data was terminated.

Let’s try another example.

MCT-222: EMBEDDED SYSTEMS


4
ARD 2
University of Engineering and Technology, Lahore Spring 2018

Example 2_2:
/*
* Math is fun!
*/

int a = 5;
int b = 10;
int c = 20;

void setup() // run once, when the sketch starts


{
Serial.begin(9600); // set up Serial library at 9600 bps

Serial.println("Here is some math: ");

Serial.print("a = ");
Serial.println(a);
Serial.print("b = ");
Serial.println(b);
Serial.print("c = ");
Serial.println(c);

Serial.print("a + b = "); // add


Serial.println(a + b);

Serial.print("a * c = "); // multiply


Serial.println(a * c);

Serial.print("c / b = "); // divide


Serial.println(c / b);

Serial.print("b - c = "); // subtract


Serial.println(b - c);
}

void loop() // we need this to be here even though its


// empty
{
}

Try to predict the output in the serial window before running the code.
HINT: Since no code was written in the loop() function, it will execute only once.

* Examples 1 and 2 taken from http://www.ladyada.net/learn/arduino/lesson4.html

MCT-222: EMBEDDED SYSTEMS


5
ARD 2
University of Engineering and Technology, Lahore Spring 2018

How about some two way communication? Run the next example and see what happens! (you
can enter the data you want to transmit in the textbox above the display area when using serial
monitor)

Example 2_3:

/*
* Serial inputs and LEDs
*/

int led = 13; // using the LED on pin 13

void setup() // run once, when the sketch starts


{
pinMode(led,OUTPUT);
Serial.begin(9600); // set up Serial library at 9600 bps

Serial.println("Press 1 to turn on LED or 'x' to clear");


}

void loop()
{
if (Serial.available())
{
char input_read = Serial.read();
if (input_read == '1')
{
digitalWrite(led,HIGH);
Serial.print("Turned on LED at pin ");
Serial.println(led);
}
if (input_read == 'x') // if input was character x
{
digitalWrite(led,LOW);
Serial.println("Cleared");
}
}

}
Let’s try to analyze the program before running the sketch. There are two more functions used
from the serial library.
Serial.available():
Checks if some data is transmitted from the computer to the Arduino (via RX pin). Returns a
value 1 if some data is available.
Serial.read():
The read() function reads the incoming data from the serial buffer, and it can be stored in a
variable to be utilized later.
The code waits for a byte of data to come from the computer, and then compares it to either turn
ON or turn OFF and led connected to pin 13. Press ‘1’ to turn it on, and press ‘x’ to turn it off.

MCT-222: EMBEDDED SYSTEMS


6
ARD 2
University of Engineering and Technology, Lahore Spring 2018

TASK 2_1:
▪ Attach a Variable Resistor to pin A0
(Fig.2.4)
▪ Convert the analog voltage from the
pot to digital using analogRead(), and
display the result in the serial monitor
using 4800 baud rate as transmission
speed.
▪ Turn ON an LED at pin 13, when the
result becomes greater than 512 (2.5 Fig.2.4: Using Rotary Potentiometer as Analog Input
V)
(you can use the AnalogReadSerial.ino example from Arduino IDE and modify it)

TASK 2_2:
▪ Attach eight LEDs to Arduino from pins 2 to 9.
▪ Sketch Change:
o Modify Example 2_3 to take input from characters 1 through 8, and
turn ON the respective led number.
o Pressing the character ‘x’ should turn all LEDs OFF.
o Display on serial monitor (9600 bps) the LED being turned ON.

TASK 2_3: EVALUATION TASK.

▪ To be provided during the lab when first two tasks are done.
▪ Marks: Task 1 [2], Task 2 [3], Task 3 [10]

MCT-222: EMBEDDED SYSTEMS


7
ARD 2

You might also like