Introduction
As you must have seen in the video above that the lights behind my speaker react to what’s going on my screen. The lights change colors based on the color of the edges of my screen. How? Let’s get started with what is needed for the project.
Parts Needed
- Arduino Uno
- 2RGB LEDs or Strips.
- Arduino IDE setup arduino.
- And Python Set up and running.
You can pretty much get and arduino uno or nano for very cheap if you buy the clone version. If you wish to buy the real version it costs around 20USD.
RGB leds are super cheap. You can get a hundreds of them for 9USD here.
Here is a quick start guide on how to setup Python and Pip. Once you have followed the steps. Do the followind steps:
- Open CMD: Press Win+r and type CMD (Windows users only); For mac user, search “terminal” on your serach bar. And for Linux users, you’ll know what to do.
- Make sure you have pip, but typing: pip in the cmd or terminal. You’ll see a hude range of options like install , uninstall , freeze, etc. That means you have pip.
- Next type: pip install pillow.
- Then: pip install pyserial
- The finally type: pip install keyboard
Download and setup the Arduino IDE software here. This link is necessary if you don’t know how to upload the code to arduino uno.
So now we have the requirements, what now?
Coding
This is the fun part. I’ll link The github repo and then let’s go over the code together.
https://github.com/hriday111/Screen_reactive_LED
The folder in the repo called “ard” the the arduino code for the hardware required, and “main.py” is the the python code. The PC and Arduino Uno communicate through USB. The python code must be run on the PC and the arduino code uploaded to Arduino using the IDE.
Let’s go over the arduino code first:
Before you upload the code, keep note for the PORT number. For windows users it will be COMx, where x is the com number.
The code is pretty self explanatory. It arduino takes input through usb. The input should be in the format: {LED ID}{ }{LED BRIGHTNESS}
For the LEFT side led, r , g or b for the left, red green and blue leds and x y z for the right, red green and blue LEDs.
After typing the LED ID type a space and the {BRIGHTNESS VALUE}, which should be from 0-255.
Now let’s go over the python code:
#Here we import all the libraries
import keyboard #A library to check for keyboard input.
import time #Time library for, well, time managment
import ctypes
import PIL.ImageGrab # the pillow library for color detection
from keyboard import is_pressed
import winsound
import time
import serial #LIB for communicating with Arduino.
import sys
These were the above libraries that are needed for the program to run smoothly.
The code in the While loop is the main part.
It captures the screen and crops it from (0,0) to (the height of the screen, 20) so it will capture the left side of my screen and store the rgb value of each and every pixel in a list. Then it checks which rgb value was most repeated and sends that value to the arduino in the format I mentioned earlier.
Conclution
I got inspired by this project from philips HUE. I tried looking up other people who had done the project but couldn’t find any and so I decided to make my own.
Thank you for reading.
~Hriday Barot
One thought on “Screen Reactive LEDs”