IR Sensor With Micropython Using BharatPi Board

by bharatpi in Circuits > Electronics

485 Views, 0 Favorites, 0 Comments

IR Sensor With Micropython Using BharatPi Board

WhatsApp Image 2024-04-28 at 4.59.45 PM.jpeg
WhatsApp Image 2024-04-28 at 4.40.33 PM.jpeg

An infrared proximity sensor or IR Sensor is an electronic device that emits infrared lights to sense some aspect of the surroundings and can be employed to detect the motion of an object. As this is a passive sensor, it can only measure infrared radiation. This sensor is very common in the electronic industry and if you’ve ever tried to design an obstacle avoidance robot or any other proximity detection-based system, chances are you already know about this module, and if you don’t, then follow this article as here we will discuss everything about it.

In this project, we are using Bharat Pi which has both an ESP32 microcontroller and a SimCom A7672S 4G/LTE module.

Bharat Pi is an IoT prototyping board for students, innovators, startups, and developers. A simplified board with an all-in-one compute, network and storage on a single board to build rapid prototypes.


Supplies

WhatsApp Image 2024-04-28 at 4.48.06 PM.jpeg
WhatsApp Image 2024-04-28 at 4.31.19 PM.jpeg

Parts required:

Here’s a list of the parts you need,

1. ESP32

2. IR Sensor

3. USB Cable

4. Jumper wires

Pre required:

To follow this instruction you need an IDE to write and upload the code to your board, you can use uPyCraft IDE or Thonny IDE.you also need Micropython firmware installed in your ESP32 boards.

If you don't have the setup means then you can check the README file of installing and setup of uPyCraft and Thonny IDE in our Bharat Pi github page you can find the link below,

https://github.com/Bharat-Pi/MicroPython/blob/main/README.md

IR Sensor

WhatsApp Image 2024-04-28 at 4.29.00 PM.jpeg

Pinout of the IR Sensor :

IR Sensor - ESP32

VCC - Power Supply

OUT - GPIO

GND - GND

An infrared obstacle sensor module consists of an IR transmitter and an IR receiver. The

IR transmitter emits the IR signal while the IR receiver searches for the reflected IR signal

to determine if the object is present or not. The presence of obstacle is reflected in the

OUT pin:

If the obstacle is present, the sensor's OUT pin is LOW

If the obstacle is NOT present, the sensor's OUT pin is HIGH

Schematic – ESP32 With IR Sensor

WhatsApp Image 2024-04-28 at 4.40.47 PM.jpeg

In this section, we will see how to connect IR sensor with ESP32. The IR sensor will be connected with the ESP32 board with its 3 pins (GND, VCC, OUT).

Follow the schematic diagram for the ESP modules and connect them accordingly. Connect the ESP32 device with the IR sensor as shown in the schematic diagram above:

Wire the IR sensor to the ESP32 as shown in the following schematic diagram. We’re connecting the vcc pin to 3v/5v and the out pin to GPIO 23, but you can use any other suitable pins, and GND pin to GND of esp32.

ESP32 Wiring With IR Sensor :

IR Sensor - ESP32

VCC - 5V / 3V

OUT - 23 (ANY GPIO)

GND - GND


CODE Snippet

Screenshot (17).png


from machine import Pin
import time


# GPIO pin connected to the IR sensor
SENSOR_PIN = 23 


def setup():
    global ir_sensor
    # Initialize the IR sensor pin
    ir_sensor = Pin(SENSOR_PIN, Pin.IN)


def loop():
    
    # Read the state of the IR sensor
    sensor_state = ir_sensor.value()


    # Check if the sensor state has changed
    if sensor_state == 0:
        print("Obstacle detected")
    else:
        print("Obstacle cleared")


    # Delay for a short period before reading again
    time.sleep(3)


# Initialize the setup
setup()


while True:
    loop()

Demonstration

Screenshot (18).png