Lab 2
Lab 2
2) delay() :
delay means to pause the program from running for a specific time(which is
specified by us). This time is set in milliseconds.
3) digitalRead() :
This function is used to read digital inputs (i.e, 0 or 1) from the digital pins of
Arduino.
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.
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.
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.
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.
Serial Monitor is a type of display hub on which you can display and print analog
inputs to Arduino from sensors or other devices.
void setup()
void loop() {
void setup()
void loop() {
void setup()
void loop()
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/