Outside of home COVID-19. Inside at home COVID-16. Whole world is fighting against COVID-19. But for COVID-16, I am all alone. I am tired of saying "please stay away from the screen". So thought of this concept project to avoid brain infection at home.

By the way, I call two of my kids (age 1 and 6) as COVID-16 as they are equally out of control.

Project Concept

Circuit built using microcontroller (ESP32) and ultrasonic distance censor (HC-SR04) will detect the distance of the nearest obstacle. If the measured distance is less than the preconfigured threshold (say 1 meter), microcontroller will call smart TV API (e.g. in my case Roku TV API to toggle Play/Pause  http://$ROKU_DEV_TARGET:8060/keypress/play) to perform the desired activity.

Project Concept Diagram

Note:  For my  Roku TV, API is there to trigger different remote button functions.

Tools,Technologies and Components used in this article

Build the Circuit

Connect all the components as shown in the diagram below.

Schematic Diagram:

circuit schematic diagram

Circuit Design:



  1. Connect ESP32 development board to your computer.
  2. Open Thonny Python IDE.
  3. Select Run --> Select interpreter...
  4. Choose MicroPython (ESP32) as device and corresponding port.
    MicroPython (ESP32) interpreter and Port
  5. Upload following python libraries
  6. Upload the below "main.py" (vanilla version). Feel free to update as per your requirement. For my Roku TV Rest endpoint to simulate "Play/Pause" remote button is
    Roku keypress commands: 'home', 'rev', 'fwd', 'play', 'select', 'left', 'right', 'down', 'up', 'back', 'replay', 'info', 'backspace', 'enter', 'volumeDown', 'volumeUp', 'volumeMute', 'inputTuner', 'inputHDMI1', 'inputHDMI2', 'inputHDMI3', 'inputHDMI4', 'inputAV1', 'channelUp', 'channelDown'

import wifimgr
from hcsr04 import HCSR04
from machine import Pin,I2C
import ssd1306,time
import urequests

def init():
    i2c = I2C(scl=Pin(19), sda=Pin(18))
    oled = ssd1306.SSD1306_I2C(128, 64, i2c, 0x3c)
    sensor = HCSR04(trigger_pin=32, echo_pin=35, echo_timeout_us=1000000)

    return oled, sensor

def log(msg, x, y):
    oled.text(msg, x, y)

def clearDisplay():

def toggleTvPlayPause():
        response = urequests.post("")
        print("API Response %s" % response.status_code)
        return response.status_code == 200
        print("Error !!!")
        log("Error !!!", 0, 40)
        return False

# Get OLED display & Distance sensor
oled, sensor = init()

# Connect to WIfi
log("Connecting...", 0, 0)
wlan = wifimgr.get_connection()
if wlan is None:
    log("No wifi !!!", 0, 20)
    print("Unable to connect to Wifi")
    log("Connected :-)", 0, 20)
    print("Deactivated AP mode.")


prevDistance = -1
isPaused = False

while True:
    currDistance = int(sensor.distance_cm())

    if currDistance != prevDistance:
        print("Distance: %s cm, isPaused: %s" %(currDistance, isPaused))
        log("Dist: %s cm" % currDistance,0, 0)

        prevDistance = currDistance

        if currDistance < 100:
            if not isPaused and toggleTvPlayPause():
                isPaused = True
            if isPaused and toggleTvPlayPause():
                isPaused = False

        print("TV -> %s" % ("Pause" if isPaused else "Play"))
        log("TV -> %s" % ("Pause" if isPaused else "Play"), 0, 40)
  1. Don't update "boot.py" unless want to do anything specific interesting.
    MicroPython ESP32 device python files

Note: I have used web based connection manager (WiFi Manager) to setup Wifi Connectivity in ESP32. This will save wifi usernamee & password in "profile.dat" (for me NETWORK_PROFILES='profile.dat' in wifimgr.py).


Due to home isolation, screen time got increased manifolds.  I was really worried. But initial version seems working for them.

Circuit In Action

smart tv distance sensor in action

Once they will figure out the trick (if they manually flip the play/pause using remote, it may work in their favour as Roku API only toggles the button), I may need to think about next version.

Video Demo:

Possible Extension

Following four may be good addition to enhance this project.

  1. Buzzer to make noise along with "Pause" as an additional cue.
  2. ESP32-CAM for face detection / recognition / emotion / gesture based action.
  3. PIR motion sensor to pause or switch off TV, if nobody is watching. (Kid's Eye and Energy Safe Smart and Green TV - Part 2)
  4. Post analytics in cloud and visualize kid's behaviour.

Download SrcCodes

All codes used in this post are available on Github: srccodes/smart-tv-distance-sensor.