Control LED on EDU:BIT Using IoT Blynk App and Raspberry Pi

Control LED on EDU:BIT Using IoT Blynk App and Raspberry Pi

Introduction

Continue from previous tutorial, since the Raspberry Pi has WiFi, I will try to control the LEDs in EDU:BIT using the Blynk app. The idea is, I will program the Raspberry Pi to read data on the Blynk server, and then send the data to EDU:BIT via serial USB communication. Then the EDU:BIT will control the LED based on the data received.

Video

This video shows how to control LED on EDU:BIT using IoT Blynk app and Raspberry Pi.

Hardware Preparation

This is the list of items used in the video.

Sample Program

This is python sample program for micro:bit (top) and Raspberry Pi (bottom). You can use mu editor to program micro:bit and Thonny Python IDE to run program in Raspberry Pi. Please install python Blynk library with following command.

sudo pip3 install blynk-library-python
from microbit import *
import music
music.play(["C4:1", "G4:1"])
pot_value = 0
pot_prev = 0
data_in = ''
while True:
# Read serial uart data from Raspberry Pi and control LEDs
try:
data_in = uart.read()
#display.scroll(data_in)
pin16.write_digital(int(data_in) & 1)
pin15.write_digital((int(data_in) & 2) >> 1)
pin14.write_digital((int(data_in) & 4) >> 2)
except:
pass

from gpiozero import LED, Button, Buzzer
from time import sleep
import serial
import BlynkLib
# Replace with your microbit port number
PORT = "/dev/ttyACM0"
BAUD = 115200
s = serial.Serial(PORT)
s.baudrate = BAUD
s.parity = serial.PARITY_NONE
s.databits = serial.EIGHTBITS
s.stopbits = serial.STOPBITS_ONE
s.timeout = 1
s.reset_input_buffer()
BLYNK_AUTH = 'Your Auth Blynk Token'
blynk = BlynkLib.Blynk(BLYNK_AUTH)
# Register virtual pin handler
@blynk.VIRTUAL_WRITE(0)
def v0_write_handler(value):
global ledStatus
global led1State
led1State = int(value[0])
print("V0: {}".format(led1State))
if led1State == 1:
ledStatus = ledStatus + 1
else:
ledStatus = ledStatus 1
@blynk.VIRTUAL_WRITE(1)
def v1_write_handler(value):
global ledStatus
global led2State
led2State = int(value[0])
print("V1: {}".format(led2State))
if led2State == 1:
ledStatus = ledStatus + 2
else:
ledStatus = ledStatus 2
@blynk.VIRTUAL_WRITE(2)
def v2_write_handler(value):
global ledStatus
global led3State
led3State = int(value[0])
print("V2: {}".format(led3State))
if led3State == 1:
ledStatus = ledStatus + 4
else:
ledStatus = ledStatus 4
led1State = 0
led2State = 0
led3State = 0
ledStatus = 0
prevLedStatus = 1
blynk.virtual_write(0, 0)
blynk.virtual_write(1, 0)
blynk.virtual_write(2, 0)
while True:
blynk.run()
if ledStatus != prevLedStatus:
print("LED Status: {0:03b}".format(ledStatus))
s.write(str(ledStatus).encode('UTF-8'))
prevLedStatus = ledStatus
s.close()

view raw
RPiBlynkSerial.py
hosted with ❤ by GitHub

Thank You

References:

Thanks for reading this tutorial. If you have any technical inquiries, please post at Cytron Technical Forum.

Please be reminded, this tutorial is prepared for you to try and learn.
You are encouraged to improve the code for better application.

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

Read and Log Internal Temperature Data in Raspberry Pi Pico to .txt file.
3.3V Serial LCD with Raspberry Pi Pico via SPI or I2C
Read and Display Environment Sensor Data Using Raspberry Pi Pico and CircuitPython
Control LED Brightness Using PWM On Raspberry Pi Pico
Overclock Raspberry Pi 4 Model B to 2.0GHz for Raspberry Pi OS
Scroll to Top