Line Follower Part 2

Lesson 2: Introduction to Sensors

Components:

  • 1x IR sensor module (line detection)
  • 1x ESP32
  • Jumper wires
  • Breadboard

Code:

This code reads the values from the IR sensor and displays the readings in the terminal. Students can move black and white paper under the sensor to observe changes.

from machine import Pin, ADC
import time

# Define the sensor input
sensor_pin = ADC(Pin(34))  # Replace with the GPIO pin you're using
sensor_pin.atten(ADC.ATTN_11DB)  # Set range of ADC

while True:
    sensor_value = sensor_pin.read()
    print("Sensor Value:", sensor_value)
    time.sleep(0.5)

Explanation:

  • sensor_pin.read() reads the value from the IR sensor.
  • The values will differ when detecting black (low value) or white (high value).

Leave a Comment

Your email address will not be published. Required fields are marked *