Lesson 2: Analog Read Serial
Lesson 2: Analog Read Serial
Lesson: Analog Read Serial
Objective: At the end of this lesson, you will be able to read analog input from a potentiometer and display the values on the serial monitor using an Arduino board.
Materials Required:
- Arduino board
- Potentiometer
- Jumper wires
- USB cable
Circuit Connections:
- Connect one terminal of the potentiometer to the 5V pin on the Arduino board.
- Connect the other terminal of the potentiometer to the ground (GND) pin on the Arduino board.
- Connect the center terminal (wiper) of the potentiometer to analog pin A0 on the Arduino board.
Arduino Code:
void setup() {
Serial.begin(9600); // Initialize the serial communication
}
void loop() {
int sensorValue = analogRead(A0); // Read the analog input from pin A0
Serial.println(sensorValue); // Print the value on the serial monitor
delay(500); // Delay for 0.5 seconds
}
Experiment Steps:
- Set up the circuit connections as described above.
- Connect the Arduino board to your computer using a USB cable.
- Open the Arduino IDE software and create a new sketch.
- Copy and paste the provided code into the Arduino IDE.
- Click the "Upload" button to upload the code to the Arduino board.
- Open the Serial Monitor by clicking the magnifying glass icon in the Arduino IDE toolbar.
- Ensure the baud rate in the Serial Monitor is set to 9600.
- Rotate the potentiometer and observe the analog values displayed on the serial monitor, ranging from 0 to 1023.
Explanation:
The Arduino code above uses the Serial.begin()
function to initialize the serial communication with a baud rate of 9600. The analogRead()
function is used to read the analog input from pin A0, which is connected to the potentiometer. The obtained sensor value is then printed on the serial monitor using the Serial.println()
function. The delay()
function is used to introduce a delay of 500 milliseconds between readings.
By rotating the potentiometer, you change its resistance, and the analog values read by the Arduino will vary accordingly. These values are displayed on the serial monitor, allowing you to observe the changes in real-time.
Challenge: Can you modify the code to map the analog values to a specific range, such as displaying the values between 0 and 100 instead of 0 and 1023?
Comments
Post a Comment