Steps to Create a Button controlled LED Project
1
Open OmniBoard Studio and create a new project.
2
Drag and drop the necessary blocks to create a simple circuit that controls an LED with a button.
Try to explore and make the diagram you self or click here and find out one of the solutions. ▼
- ●Place Start block.
- ●Connect the Start block to a While True block to create an infinite loop.
- ●Then connect the While True block to a Button block. This will allow us to check the state of the button.
- ●Finally connect the True output of the Button block to a LED ON block. This will cause the LED to turn on. To the False output connect a LED OFF block. This will cause the LED to turn off.
3
Make the correct circuit
- ●Connect the button to the correct GPIO pin on your microcontroller. This will allow the Button block to read the state of the button correctly. Your usual button will have two pairs of pins. You will need to connect one pair to a GPIO pin and the other pair to 3.3V.
- ●Now you can try to connect the LED from memory because the circuit is the same as in the previous tutorial. Or click to reveal the circuit diagram and instructions.
- ●Connect the GPIO pin asigned to the LED to the rezistor.
- ●Connect the positive leg of the LED to a resistor, and then connect the other end of the LED to ground. You can determine which leg is positive and which is negative by finding the longer leg (positive) and shorter leg (negative) or flat side of the LED cover (negative).
Click to reveal the circuit diagram and instructions ▼
4
Compile the code and upload it to your microcontroller.
5
Watch your LED blink on and off at the interval you set with the Timer block!
Bonus informations
- Here is an example of simplified the MicroPython code that would be generated by the blocks for a button controlled LED project:
from machine import Pin # Import the Pin class to control GPIO pins
led = Pin(2, Pin.OUT) # Set GPIO pin 2 as an output for the LED
button = Pin(0, Pin.IN, Pin.PULL_UP) # Set GPIO pin 0 as an input for the button with a pull-up resistor
while True: # Start an infinite loop
if button.value() == 1: # Check if the button is pressed (active high)
led.value(1) # Turn the LED on
else: # If the button is not pressed
led.value(0) # Turn the LED off
import RPi.GPIO as GPIO # Import the GPIO library to control GPIO pins
GPIO.setmode(GPIO.BCM) # Set the GPIO mode to BCM
GPIO.setup(2, GPIO.OUT) # Set GPIO pin 2 as an output for the LED
GPIO.setup(0, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set GPIO pin 0 as an input for the button with a pull-up resistor
while True: # Start an infinite loop
if GPIO.input(0) == GPIO.HIGH: # Check if the button is pressed (active high)
GPIO.output(2, GPIO.HIGH) # Turn the LED on
else: # If the button is not pressed
GPIO.output(2, GPIO.LOW) # Turn the LED off