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

Lab 2

The document discusses several Arduino functions including delay(), digitalRead(), analogRead(), analogWrite() and serial commands. It explains how to use these functions through examples and provides details on reading analog values from a potentiometer and printing them to the serial monitor.

Uploaded by

Blank User
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lab 2

The document discusses several Arduino functions including delay(), digitalRead(), analogRead(), analogWrite() and serial commands. It explains how to use these functions through examples and provides details on reading analog values from a potentiometer and printing them to the serial monitor.

Uploaded by

Blank User
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Arduino IDE Exp-2

2) delay() :

This is one of the most commonly used functions in Arduino programming as


delays are required in almost every automation project.

delay means to pause the program from running for a specific time(which is
specified by us). This time is set in milliseconds.

Example: ” delay(1000) ” // pause the program for 1s or 1000ms

” delay(5000) ” // pause the program for 5s or 5000ms

3) digitalRead() :

This function is used to read digital inputs (i.e, 0 or 1) from the digital pins of
Arduino.

Example: ” digitalRead(13) ” // Read digital input from pin 13

NOTE: HIGH means Logic 1 or 5 volts whereas LOW means Logic 0 or 0 volts

4) analogRead() :

This function is used to read analog inputs (i.e, value between 0 and 1023 or 0
volts and 5 volts) from the analog pins of Arduino.

Example: ” analogRead(A0) ” // Read analog input from analog pin A0

NOTE A: analogRead function can be used only on Analog pins.

NOTE B: analog value 0 means 0 volts and analog value 1023 means 5 volts

5) analogWrite() :
This function is used to write analog voltage value (i.e, any value between 0 and
255 or 0 volts and 5 volts) on the digital PWM pins of Arduino i.e, to give PWM
output on PWM pins.

The analogWrite function is only applicable to particular digital pins of Arduino


Boards called PWM pins. For example, PWM pins of Arduino UNO are 3, 5, 6, 9,
10, and 11.

NOTE : PWM value 0 means 0 volts and PWM value 255 means 5 volts.
Similarly, PWM value 127 gives approximately 2.5 volts on PWM pins.

Example A: ” analogWrite(3,0) ” // Gives output 0 volts to digital pin 3

Example B: ” analogWrite(3,255) ” // Gives output 5 volts to digital pin 3

Example C: ” analogWrite(3,127) ” // Gives output 2.5 volts to digital pin 3

Serial Commands in Arduino Programming:

Serial communication takes place between the Arduino board and Arduino IDE
installed on a laptop. So to print any input data (sensor, keyboard) on Arduino IDE,
serial commands are required.

What is Serial Monitor in Arduino IDE?

Serial Monitor is a type of display hub on which you can display and print analog
inputs to Arduino from sensors or other devices.

1. “Serial.begin(9600)“: this command is used to initialize serial communication


between Arduino and Arduino IDE. This command must be typed inside void
setup() before using any other serial commands.

2. “Serial.print(variable whose value is to be printed)”: this command simply prints


the required value onto the serial monitor. But remains on the same line. Let’s see
an example to understand this more clearly:
Example:

void setup()

Serial.begin(9600); // begin serial communication

void loop() {

Serial.print("Hey");// print Hey on serial monitor

** This program prints “Hey” on the serial monitor

void setup()

Serial.begin(9600); // begin serial communication

void loop() {

Serial.print("Hey");// prints Hey on serial monitor

Serial.print("There");// prints There on serial monitor

** This program prints “HeyThere” on Serial Monitor


As you see even though the values to be printed uses the different print commands,
both are printed one by one or side by side. To solve this error Serial.println() is
used.

3. “Serial.println(variable whose value is to be printed)”: this command simply


prints the required values on the next line onto the serial monitor. Let’s see an
example to understand this more clearly:

void setup()

Serial.begin(9600); // begin serial communication

void loop()

Serial.println("Hey");// prints Hey on serial monitor

Serial.println("There");// prints There on serial monitor

**This program outputs: Hey

There

void setup() {
Serial.begin(9600);
}

void loop() {
Serial.println("aaaaa");
delay(300);
}
Potentiometer: It’s a variable resistor has 3 pins, taking the middle pin as an
output make it easy to get opposite action (increasing or decreasing) the
resistor value. As shown in figure below.
 Read analog value from potentiometer middle pin
-> value=analogRead(potPin)
 Map analog values 0-1024 to pwm values 0-255
-> value = map(value, 0, 1023, 0, 255);
 Send pwm value to led
-> analogWrite(ledPin, value);
 int v;
 void setup() {
 pinMode(7, OUTPUT);
 Serial.begin(9600);
 }
 void loop() {
 v= analogRead(3);
 v= map(v,0,1024,0,255);
 analogWrite(A2, v);
 }
Printout on serial monitor:

 int v;
 void setup() {
 pinMode(7, OUTPUT);
 Serial.begin(9600);
 }
 void loop() {
 v= analogRead(3);
 v= map(v,0,1024,0,255);
 analogWrite(A2, v);
 Serial.print(v);
 delay(400);
 Serial.println("\n");
 delay(500);
 }
https://www.makerspaces.com/simple-arduino-projects-beginners/

You might also like