Professional Documents
Culture Documents
Please read through all the suggested reading before you come to lab.
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)
Example 2_1:
/*
* Hello World!
*
* This is the Hello World! for Arduino.
* It shows how to send data to the computer
*/
Upload the Example code to the Arduino, and turn on the serial monitor by
clicking on the button (Fig.2.3).
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.
Example 2_2:
/*
* Math is fun!
*/
int a = 5;
int b = 10;
int c = 20;
Serial.print("a = ");
Serial.println(a);
Serial.print("b = ");
Serial.println(b);
Serial.print("c = ");
Serial.println(c);
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.
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
*/
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.
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.
▪ To be provided during the lab when first two tasks are done.
▪ Marks: Task 1 [2], Task 2 [3], Task 3 [10]