Raspberry Pi driven RGB LED strips

michaeljtbrooks, updated 🕥 2022-11-12 23:03:45

Raspiled

Raspberry Pi driven RGB LED strips (updated 08 Sep 2018: bugfixes and faster page rendering)

Raspiled Web Interface

What is this?

RaspiLED is a Python based controller for LED strips. It allows you to drive full-colour (RGB) LED strip from a Raspberry Pi, via a very easy to use touch-friendly web interface.

  • Control LED strip mood lighting with your smartphone
  • Pre-programmed with main colours
  • Simulated sunrises and sunsets
  • Colour-sequences such as party lighting

Based on a tutorial from David Ordnung: https://dordnung.de/raspberrypi-ledstrip/

Disclaimer: I am not responsible if you nuke your Raspberry Pi. Do this at your own risk. Although at £30 / $40 a pop it's hardly much of a risk!

Requirements

  1. Python & this repository
  2. A network capable Raspberry Pi or Pi Zero W
  3. LED strips, we recommend the SMD5050 RGB type. Check out AliExpress
  4. 3 x MOSFETs 3.3v logic compatible. I suggest IRLZ34N
  5. Prototyping matrix board / PCBs
  6. 4 core RGB LED ribbon cable
  7. Female jumper headers so you can connect your Raspberry Pi to your break out board
  8. 12V DC power supply to drive the LEDs (many come with one)
  9. 5V DC power supply to drive the Raspberry Pi
  10. Pigpio to provide you with software pulse width modulation
  11. (optional) 3 x 100k pull-down resistors (yes, I know the Raspberry Pi can pull down its own pins, but I like to have my own pull-down resistors, which are safe from accidental software command slippage!)

Hardware circuitry

A relatively simple circuit binds the Raspberry Pi to the LED strip. We use three N-type MOSFETs, one for each colour channel (red / green / blue). The MOSFETs are essentially voltage controlled switches, which thanks to their enormous gate to source resistance, protect the Raspberry Pi pins. MOSFETs can also switch at high frequency, which is important as we're using Pulse Width Modulation (PWM), which involves flicking the LED channels on and off very quickly to adjust their brightness.

I followed David Ordnung's recommendation and went with IRLZ34N MOSFETs. These can handle enough current through the drain > source to run several full size LED strips, are switched on fully at 3.3V on the gate (logic high for Raspberry Pis), and can switch on and off fast enough. You do NOT need a base resistor between the MOSFET gate and the Raspberry Pi pin because MOSFETs have a very high Gate > Source resistance (unlike a regular NPN transistor where you would need one).

MOSFET connections: * Left pin (gate), is connected to a Raspberry Pi GPIO pin * Middle pin (drain), goes to the colour channel connector on the LED strip * Right pin (source), is connected to ground

What we're doing is using the Raspberry Pi pins to switch the MOSFET on, thus connecting the negative side of the respective LED colour channel to ground, allowing those LEDs to turn on.

Here's the circuit I used:

Raspiled circuit

For clarity, the way the SMD5050 LED strips work is that they take a common 12V power in, and you connect the relevant colour channel to GROUND to switch the LED on. So the pad marked + on the strip goes to 12V. The pads marked R/G/B go to ground via our MOSFETs. Don't try to stick 12V into the R / G / B contact pads on the LED strip, it won't like it.

Optional stuff

The LED strips take 12V, but the Raspberry Pi is driven by 5V. I didn't want to muck about with two different "wall wart" power supplies, so I bought a little 12V DC to 5V DC buck converter, and used the output of that to power the Raspberry Pi. Btw you CAN power the Raspberry Pi by pumping 5V into its 5V pin, just be aware that this bypasses the thermal fuse. I made sure my 12V power supply was high quality.

You don't need a heatsink for the MOSFETs, unless your house happens to be hotter than the Sahara (literally hotter than 50 degrees Celcius). I've calculated that the MOSFETs can dissipate three times the heat they will generate when on full tilt at 12V, without a heatsink. That said, they do get hot to the touch, so heatsinks are a consideration if you're stuffing them in a tight enclosed space.

Software Installation

  1. Get Raspian or Ubuntu running on your Raspberry Pi, with network connectivity working, and install essential packages: bash sudo apt-get install build-essential unzip wget git python3-dev liblapack-dev libatlas-base-dev
  2. Install pigpio (see http://abyz.me.uk/rpi/pigpio/download.html) bash wget https://github.com/joan2937/pigpio/archive/master.zip unzip master.zip cd pigpio-master make sudo make install
  3. Download this Raspiled repo to your Raspberry Pi
  4. SSH into your Raspberry Pi. Change to the directory where you saved this repo
  5. Install python virtual environments bash sudo apt-get install python-pip sudo pip3 install venv
  6. Create a virtual environment to run Raspiled in, and activate it bash python3 -m venv ./venv source ./venv/bin/activate
  7. Install this repo's dependencies (may take 1- mins on a Raspberry Pi bash pip install -r ./src/requirements.txt
  8. Find out your Raspberry Pi's IP address: bash ifconfig
  9. Copy ./src/raspiled.conf.TEMPLATE to ./src/raspiled.conf so Raspiled knows what settings to use.
  10. Modify ./src/raspiled.conf: change the constants for the Pins match the GPIO pins you have connected the LED colour channels to, and change PI_HOST to "localhost". PI_PORT should be left as 8888 as this is what Pigpiod is configured to use.
  11. Run the Pigpiod daemon: bash sudo pigpiod
  12. Run the Raspiled server: bash python ./src/raspiled_listener.py
  13. On your smartphone / another computer on the same local network, open your web browser and head to: http://:9090 e.g. http://192.168.0.33:9090 in my case
Optional stuff

If you want the Raspberry Pi to boot up and automatically run Raspiled, you can add this command to /etc/rc.local: bash /path/to/your/virtualenv/python3 /path/to/your/raspiled/src/raspiled_listener.py Use absolute paths. e.g. assuming you put Raspiled into the /opt directory: bash /opt/raspiled/venv/bin/activate/python /opt/raspiled/src/raspiled_listener.py

Web Interface

http://:9090

Pretty self explanatory. Click a colour to change the LEDs to that colour. Play with the colour wheel and brightness slider to manually control the colour. Press one of the sequence buttons to start the LEDs running that colour sequence. Click "Off" to set the LEDs to black (i.e. no light).

Raspiled Web Interface

I've used a very lightweight Python Twisted webserver for this, because I couldn't be bothered with fannying around with configuring Apache2 or Nginx. There's no authentication as this is not supposed to be exposed to the dirty internet WAN.

I've spent a fair bit of time getting the threading right so that you can start and stop sequences without a delay. So while Raspiled is busy running the colour changes of a sequence, it will still respond immediately to new commands, even if it is mid-fade between colours.

That's it!

Feel free to download the code, dick about with it, make something awesome. I am also keen to have people contribute to this repo, so long as what you've written is readable!

Here are some ideas for improvements: * Authentication & tokens: so only recognised devices can control the LEDs * Sound responsiveness: So the LED lights flash to the beat! * IFTTT events and notifications: e.g. flash green when your smartphone / WhatsApp / Skype rings. * Binding to other IoT stuff: e.g. flash when someone rings the doorbell or run the NeeNaw USA sequence when your house alarm sounds! * Scheduled events: e.g. run the sunset sequence over the hour before bedtime * Holiday mode: turn the lights on for several minutes then off to deter burglars

With thanks to

Issues

Fix Python 3 compatibility, drop Python 2 support

opened on 2021-12-28 00:21:07 by zvyn

Drops references to unicode, changes __unicode__ to str, fix comparisons between bytes and str.

This brakes Python 2 compatibility.

I started this mainly because Debian 11 comes without Python 2 by default.

Bump pygments from 2.5.2 to 2.7.4 in /src

opened on 2021-11-05 23:07:23 by dependabot[bot]

Bumps pygments from 2.5.2 to 2.7.4.

Release notes

Sourced from pygments's releases.

2.7.4

  • Updated lexers:

    • Apache configurations: Improve handling of malformed tags (#1656)

    • CSS: Add support for variables (#1633, #1666)

    • Crystal (#1650, #1670)

    • Coq (#1648)

    • Fortran: Add missing keywords (#1635, #1665)

    • Ini (#1624)

    • JavaScript and variants (#1647 -- missing regex flags, #1651)

    • Markdown (#1623, #1617)

    • Shell

      • Lex trailing whitespace as part of the prompt (#1645)
      • Add missing in keyword (#1652)
    • SQL - Fix keywords (#1668)

    • Typescript: Fix incorrect punctuation handling (#1510, #1511)

  • Fix infinite loop in SML lexer (#1625)

  • Fix backtracking string regexes in JavaScript/TypeScript, Modula2 and many other lexers (#1637)

  • Limit recursion with nesting Ruby heredocs (#1638)

  • Fix a few inefficient regexes for guessing lexers

  • Fix the raw token lexer handling of Unicode (#1616)

  • Revert a private API change in the HTML formatter (#1655) -- please note that private APIs remain subject to change!

  • Fix several exponential/cubic-complexity regexes found by Ben Caller/Doyensec (#1675)

  • Fix incorrect MATLAB example (#1582)

Thanks to Google's OSS-Fuzz project for finding many of these bugs.

2.7.3

... (truncated)

Changelog

Sourced from pygments's changelog.

Version 2.7.4

(released January 12, 2021)

  • Updated lexers:

    • Apache configurations: Improve handling of malformed tags (#1656)

    • CSS: Add support for variables (#1633, #1666)

    • Crystal (#1650, #1670)

    • Coq (#1648)

    • Fortran: Add missing keywords (#1635, #1665)

    • Ini (#1624)

    • JavaScript and variants (#1647 -- missing regex flags, #1651)

    • Markdown (#1623, #1617)

    • Shell

      • Lex trailing whitespace as part of the prompt (#1645)
      • Add missing in keyword (#1652)
    • SQL - Fix keywords (#1668)

    • Typescript: Fix incorrect punctuation handling (#1510, #1511)

  • Fix infinite loop in SML lexer (#1625), CVE-2021-20270 <https://nvd.nist.gov/vuln/detail/CVE-2021-20270>_

  • Fix backtracking string regexes in JavaScript/TypeScript, Modula2 and many other lexers (#1637) CVE-2021-27291 <https://nvd.nist.gov/vuln/detail/CVE-2021-27291>_

  • Limit recursion with nesting Ruby heredocs (#1638)

  • Fix a few inefficient regexes for guessing lexers

  • Fix the raw token lexer handling of Unicode (#1616)

  • Revert a private API change in the HTML formatter (#1655) -- please note that private APIs remain subject to change!

  • Fix several exponential/cubic-complexity regexes found by Ben Caller/Doyensec (#1675)

  • Fix incorrect MATLAB example (#1582)

Thanks to Google's OSS-Fuzz project for finding many of these bugs.

Version 2.7.3

(released December 6, 2020)

... (truncated)

Commits
  • 4d555d0 Bump version to 2.7.4.
  • fc3b05d Update CHANGES.
  • ad21935 Revert "Added dracula theme style (#1636)"
  • e411506 Prepare for 2.7.4 release.
  • 275e34d doc: remove Perl 6 ref
  • 2e7e8c4 Fix several exponential/cubic complexity regexes found by Ben Caller/Doyensec
  • eb39c43 xquery: fix pop from empty stack
  • 2738778 fix coding style in test_analyzer_lexer
  • 02e0f09 Added 'ERROR STOP' to fortran.py keywords. (#1665)
  • c83fe48 support added for css variables (#1633)
  • Additional commits viewable in compare view


Dependabot compatibility score

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


Dependabot commands and options
You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself) - `@dependabot use these labels` will set the current labels as the default for future PRs for this repo and language - `@dependabot use these reviewers` will set the current reviewers as the default for future PRs for this repo and language - `@dependabot use these assignees` will set the current assignees as the default for future PRs for this repo and language - `@dependabot use this milestone` will set the current milestone as the default for future PRs for this repo and language You can disable automated security fix PRs for this repo from the [Security Alerts page](https://github.com/michaeljtbrooks/raspiled/network/alerts).

Launching issue on Ubuntu 21.04

opened on 2021-06-06 13:39:41 by revoxhere

Hey there, I'm not sure if this project is still maintained, but I was using Raspiled before for half a year on Ubuntu Server 18.04, and all worked fine. Problems stared appearing when I updated my pi to Ubuntu Server 21.04, that's the issue I'm facing: ```PYTHON (raspiled) [email protected]:~/raspiled$ python ./src/raspiled_listener.py [13:40:00 RASPILED] Using config file: /home/ubuntu/raspiled/src/raspiled.conf [13:40:01 RASPILED] [STARTING] Raspiled Listener with PID 43645 [13:40:01 RASPILED] Cannot read PWM of pin #27 [13:40:01 RASPILED] Cannot read PWM of pin #27 [13:40:01 RASPILED] Cannot read PWM of pin #27 Unhandled error in Deferred:

Traceback (most recent call last): File "/home/ubuntu/raspiled/./src/raspiled_listener.py", line 831, in start_if_not_running() File "/home/ubuntu/raspiled/./src/raspiled_listener.py", line 823, in start_if_not_running endpoint.listen(factory) File "/home/ubuntu/raspiled/lib/python3.9/site-packages/twisted/internet/endpoints.py", line 511, in listen return defer.execute( --- --- File "/home/ubuntu/raspiled/lib/python3.9/site-packages/twisted/internet/defer.py", line 139, in execute result = callable(args, *kw) File "/home/ubuntu/raspiled/lib/python3.9/site-packages/twisted/internet/posixbase.py", line 569, in listenTCP p.startListening() File "/home/ubuntu/raspiled/lib/python3.9/site-packages/twisted/internet/tcp.py", line 1327, in startListening skt.bind(addr) builtins.TypeError: an integer is required (got type str)

``` I'm not sure what's the issue here, maybe the repository or some dependencies don't work on Python 3.10? I'll keep looking myself, but any help will be greatly appreciated.

Merge issues solved

opened on 2018-09-15 15:11:26 by josuemtzmo

Hello Michael,

I solved the issues conflicting with the merge, also I solved some of the issues with the temperature of sunsets and sunrise, however, we will need to calibrate the time again, because they are not consistent.

Cheers, Josue

Incorporating Josue's code with Mike's amendments

opened on 2018-09-08 18:04:19 by michaeljtbrooks None

Releases

Improvements to Py2-Py3 compatibility. Better Tasty Teal 2021-11-06 10:36:11

Raspiled 2021-06-06 13:59:21

Dr Michael Brooks

Emergency Medicine doctor, tech entrepreneur and Python code tinkerer.

GitHub Repository