Controlling the LED brightness


#1

This post is to share a bit more details on how to control LED brightness using Raspberry PI and a bit of python code.

First of all, all the credit goes to Ingmar Stapel who posted all the details on his website https://custom-build-robots.com/ on how to setup LED dimmer and recently added the wiring diagram that makes it very easy to reproduce.

Here’s a link to the blog post:
https://custom-build-robots.com/top-story-en/raspberry-pi-led-dimmer-pca9685-servo-controller/8840?lang=en

And this is the link to the Youtube video:

The python code is rather simple. Below is the example of LED brightness starting at 100% and gradually reduced to 0%:

import L298NHBridgePCA9685 as HBridge
import time

channel_left = 1 
channel_right = 1

while (channel_left >= 0): 
   print("Brigthness:" + str(channel_left*100) + "%")
   channel_left = channel_left - 0.01
   channel_right = channel_right - 0.01
   HBridge.setMotorLeft(channel_left)
   HBridge.setMotorRight(channel_right)
   time.sleep(0.1)

HBridge.setMotorLeft(0)
HBridge.setMotorRight(0)
HBridge.exit ()

Note that the code above required L298NHBridgePCA9685 that Ingmar wrote and shared on GitHub:

The Raspberry Pi Zero W costs £12.82 (+delivery, in my case £2.99). It’s from here:

The L298N DC Motor Driver Module costs $2.30 (free delivery to the UK). It’s from here:

The 16-Channel 12-bit PWM/Servo Driver costs $1.83 (+delivery, in my case $0.21). It’s from here:

The I2C Logic Level Converter costs $0.99 (5 units, +delivery to the UK $0.15). It’s from here:

The LED strip is 12V, and we purchased a variety of them with prices ranging from $2.30 to $11.05 for the 5-meter piece. Here’s one example:

The code is available here: