Sensor

๐ŸŽจ Analog Grayscale Sensor

๐Ÿ“Œ Description

The analog grayscale sensor detects how much light is reflected from a surface. Dark colors (like black) reflect less light, while bright colors (like white) reflect more. The sensor converts this into an analog value that the microcontroller can read.

๐ŸŽฏ Use

In our projects, the grayscale sensor is used to:

  • Detect lines on the floor (lineโ€‘following robots).
  • Differentiate between light and dark surfaces.
  • Measure reflectivity for object detection.

๐Ÿ‘‰ Think of the grayscale sensor as the robotโ€™s eye for shades โ€” it sees how bright or dark something is.

Module parameters

Pin name Description
G GND (Power Input Negative)
V VCC (Power Input Cathode)
S Analog signal pins
  • Supply voltage: 3.3V/5V
  • Connection: PH2.0 terminal wire
  • Installation method: double screw fixing

๐Ÿ–ฅ๏ธ Code Explanation

import machine
import time

adc39 = machine.ADC(machine.Pin(39))   # Sensor connected to pin 39

while True:
    print('Grayscale value: ')         # Print label
    print(adc39.read())                # Print sensor reading (0โ€“4095)
    time.sleep_ms(100)                 # Wait 0.1 seconds

Step by Step

  1. import machine, time โ†’ Tools for pins and timing.
  2. adc39 = machine.ADC(machine.Pin(39)) โ†’ Pin 39 reads the analog signal from the grayscale sensor.
  3. while True: โ†’ Loop that repeats forever.
  4. print('Grayscale value: ') โ†’ Prints a label.
  5. print(adc39.read()) โ†’ Shows the sensorโ€™s value (0 = dark, 4095 = bright).
  6. time.sleep_ms(100) โ†’ Adds a short delay between readings.

๐Ÿ‘‰ Together, this program makes the sensor report brightness levels every 0.1 seconds.


โœ… Conclusion of the Test

  • If the printed values change when moving the sensor over black, white, or gray surfaces, the program is working correctly.
  • This test shows how the robot can read analog input and detect brightness differences.
  • Later, the grayscale sensor could be used for lineโ€‘following robots, surface detection, or even sorting objects by color shade.

On this page