Skip to content

5. INPUT OUTPUT DEVICE

Although i did all the required in week 4 because as an electronics engineer i have a good knowledge in using microcontrollers i am required to fill this part of the site

we focused on esp32 coding using thonny in micropython.

some examples :

Police lights using RGB-LED:

This basic example shows a system that receive and input and based on that input it gives you a specific output


from machine import Pin
import time

# Define the pins for the RGB LED and the fan
red_pin = Pin(13, Pin.OUT)    # GPIO pin for the red color
green_pin = Pin(14, Pin.OUT)  # GPIO pin for the green color
blue_pin = Pin(15, Pin.OUT)   # GPIO pin for the blue color
button_pin = Pin(20, Pin.IN)



# Function to control the RGB LED colors
def set_colors(red_state, green_state, blue_state):
    red_pin.value(red_state)
    green_pin.value(green_state)
    blue_pin.value(blue_state)

# Initial LED state
set_colors(0, 0, 0)  # All LEDs off initially

# Loop to change colors when the button is pressed
while True:
    if button_pin.value() == 0:  # Button is pressed
        for _ in range(8):  # Iterate over the sequence
            set_colors(1, 0, 0)  # Red
            time.sleep_ms(350)
            set_colors(0, 0, 1)  # Blue
            time.sleep_ms(350)

    time.sleep(0.1)  # Small delay to avoid rapid checking of the button




The result:

Adjusting police light example

We can make a small adjustment where if you push the button it will start cycling through the rgb colors, this simple adjustment can open our mind to create systems that serve a purpose like using a sensor that when receiving a specific signal it give a specific output based on the condition and the pins you are using

from machine import Pin
import time

# Define the pins for the RGB LED and the button
red_pin = Pin(13, Pin.OUT)      # GPIO pin for the red color
green_pin = Pin(14, Pin.OUT)    # GPIO pin for the green color
blue_pin = Pin(15, Pin.OUT)     # GPIO pin for the blue color
button_pin = Pin(20, Pin.IN)    # GPIO pin for the button

# RGB color states
OFF = (0, 0, 0)
RED = (1, 0, 0)
GREEN = (0, 1, 0)
BLUE = (0, 0, 1)
YELLOW = (1, 1, 0)
CYAN = (0, 1, 1)
MAGENTA = (1, 0, 1)
WHITE = (1, 1, 1)

colors = [OFF, RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA, WHITE]
current_color = 0
button_press_count = 0

# Function to set the RGB LED color
def set_color(color):
    red_pin.value(color[0])
    green_pin.value(color[1])
    blue_pin.value(color[2])

while True:
    if button_pin.value() == 0:  # Button press detected
        button_press_count += 1

        current_color += 1
        if current_color >= len(colors):
            current_color = 0

        set_color(colors[current_color])
        time.sleep(0.5)  # Delay to debounce the button press

        if button_press_count == 8:
            set_color(OFF)
            button_press_count = 0

    time.sleep(0.1)  # Small delay to avoid rapid checking of the button

The result :

Other examples

traffic light example

red_pin = Pin(13, Pin.OUT)    # GPIO pin for the red color
yellow_pin = Pin(14, Pin.OUT) 
green_pin = Pin(15, Pin.OUT)  # GPIO pin for the green color
  # GPIO pin for the blue color


red_pin.on()
yellow_pin.off()
green_pin.off()


time.sleep(5)

red_pin.off()
yellow_pin.on()
green_pin.off()


time.sleep(2)

red_pin.off()
yellow_pin.off()
green_pin.on()


time.sleep(5)

red_pin.off()
yellow_pin.off()
green_pin.off()
small tip to create a loop

To create a simple loop let couter<=n (n is number of loaps) to finish loop add in the end counter=counter+1 to end the statement must be false or if true will continuo

Like for example

counter=0
while(counter<10):


    led.on()
    buzzer.on()

    sleep(0.5)



    led.off()
    buzzer.off()

    sleep(0.5)


    counter=counter+1



fan.off()
sleep(0.01)

this code will let the fan and the buzzer work for a specific time then stop and restart without any interference .

In the end

understanding these basic concepts will open your eyes to the microcontrollers world where every thing is possible, the only limitation is the human mind.


Last update: September 7, 2024