Physical Computing - LEDs and Push Buttons

Adam Boucek profile

Adam Boucek

|

September 19th 2023

PythonESP32

In this laboratory, we will continue doing simple circuits where we get more familiar with ESP32 and Python. Instructions are given by Andrew J. Park, Ph.D..

We will use Freenove Ultimate Starter Kit for ESP32-WROVER which you can buy on Amazon. How to set up the kit and start uploading code you can find on Freenove.

Project 1 - Three LEDs

Date: 09/16/23

Three LEDs image

Instructions:

Design and construct a circuit that includes three LEDs (red, green, and blue). When your program runs, each LED turns on and off (blinks) sequentially and continually (red → green → blue → red → green → blue → …). Capture your settings and working circuit in photos and videos.

Parts:

  • ESP32-WROVER x1
  • GPIO Extension Board x1 (optional)
  • Breadboard x1
  • Jumper M/M x6
  • Green LED 1x
  • Red LED 1x
  • Blue LED 1x
  • Resistor 220Ω x3

Three LEDs schema:

Three LEDs Schematic image

Three LEDs Breadboard View:

Three LEDs Breadboard View image

Source Code:

from time import sleep_ms from machine import Pin ledGreen=Pin(2,Pin.OUT) #create LED object from pin2,Set Pin2 to output ledBlue=Pin(15,Pin.OUT) ledRed=Pin(18,Pin.OUT) arr = [ledBlue, ledGreen, ledRed] try: while True: # Infinite loop for element in arr: element.value(1) sleep_ms(200) element.value(0) except KeyboardInterrupt: pass

Three LEDs gif

Project 2 - Two push buttons and an LED

Date: 09/16/23

Two push buttons image

Instructions:

Design and construct a circuit that includes two push buttons and an LED. When one button is pushed, the LED is on while the other button is pushed, the LED is off. When the same button is pushed multiple times, it should not change the status of the LED. Capture your settings and working circuit in photos and videos.

Parts:

  • ESP32-WROVER x1
  • GPIO Extension Board x1 (optional)
  • Breadboard x1
  • Jumper M/M x6
  • Green LED 1x
  • Push buttons 2x
  • Resistor 220Ω x1
  • Resistor 10kΩ x4

Two push buttons schema:

Two push buttons Schematic image

Two push buttons Breadboard View:

Two push buttons Bread View image

Source Code:

import time from machine import Pin led = Pin(2, Pin.OUT) buttonOn = Pin(13, Pin.IN, Pin.PULL_UP) buttonOff = Pin(12, Pin.IN, Pin.PULL_UP) def reverseGPIOON(): led.value(1) def reverseGPIOOFF(): led.value(0) while True: if not buttonOn.value(): time.sleep_ms(20) reverseGPIOON() if not buttonOff.value(): reverseGPIOOFF() time.sleep_ms(20)

Result:

Two push buttons gif