Beaglebone Black LESSON 11: Dimable LED with Buttons from Python

In LESSON 10 we showed you how to create a dimable LED using the analog input from a potentiometer. In this lesson we will create a dimable LED using digital buttons. We will say we want a press of the top button to make the LED brighter, and a press of the bottom button to make the LED dimmer. In order to get going, you will need to build this circuit. If you do not already have a Beaglebone Black, you can get one HERE.

Push Buttons LED Circuit
This Circuit Controls LED Brightness from Push Buttons

In this circuit make note that we are using two 1000 ohm resistors as pull down resistors on the push buttons. It is important that these resistors be at least 1000 Ohm each. Next, notice the current limiting resistor on the LED is 330 Ohm.  We establish a ground rail on the breadboard from pin P9_2 on the Beaglebone Black. We establish our 3.3 Volt rail on the breadboard from pin P9_4 on the Beaglebone. We will use P9_14 as the PWM pin to control the LED, and we will use pins P9_23 and P9_27 as our digital input pins.

We will want a press of the top button to increase brightness and a pres of the bottom button to decrease brightness. As we discussed in Lesson 10, we want to insrease and decrease PWM signal exponentially, as this will allow the eye to perceive a smooth and linear increase in brightness.

If we want the LED to go from full off to full brightness in 10 steps, we need an equation to relate Duty Cycle to BP. BP will be a variable that will keep track of where we are. If we press the up button we increment BP by 1. If we press the down button, we decrements BP by 1. We want to start with BP=0, and the LED full off. This would be the point:

(BP,DutyCycle) = (0,0)

When the button has been pressed 10 times, we want a DutyCycle of 100%. This would be the point:

(BP,DutyCycle) = (10,100)

We now need to fit an exponential curve through these two points.

DutyCycle = C^(BP) -B

We need to figure out what the constants C and B need to be. Note DutyCycle and BP are our variables . . . they are like X and Y. We can plug our first point in and solve for B.

0 = C^0 – B

Anything raised to 0 equals 1, so the equation becomes

0 = 1 – B


Now substitute B into our equation and we get:

DutyCycle = C^(BP) -1

Now put in our second point to calculate the constant C.

100 = C^10 – 1

101 = C^10

C = tenth root of 101 = 1.5864

So, our final equation to calculate Duty Cycle is:

DutyCycle = 1.5864^(BP) – 1

With this equation we are not ready to develop our code. The video will step you through the code line by line.

5 thoughts on “Beaglebone Black LESSON 11: Dimable LED with Buttons from Python”

  1. Your videos are fantastic! I Have really enjoyed the series, especially the balloon launch! More recently, your explanation of duty cycle in the pi series was very clear and informative. Your teaching style makes it easy to understand and remember the material by just taking a few notes along with running the examples.

    Your students are very fortunate to have you as their teacher.

    Have you considered using scite as your text editor/IDE? Its a simple but powerful editor that allows you to edit and run programs without leaving the editor. It also recognizes about 30 programming languages, their syntax and keywords (using color), based on the file extension. I just installed it on my pi and it works really well using tightVNC viewer. Associating files with .py or .c extensions with scite allows you to open them with a click. It will run interpreters like python, shell, .bat and awk and will compile and execute programs in c, c++ and c#, etc, and its free. Its also mouse and cut/paste friendly. That command line gets tedious after a while when developing software.

    1. There are a number of better IDE’s to use than nano, but I really want people to learn the old school way. Later you can always take advantage of the more fancy tools, but good to learn the old way.

  2. Thanks for the videos! So I started tweaking this example for something to try. I want the LED to flash when I press the button once and stay lit when I press the button twice and brighten and dim when I press it a third time and lastly quit the program on the fourth button press. For the most part I have this working. However I started using GPIO and PWM referencing the same LED and was wondering if that causes problems? Basically, everything stops working and I can’t get the LED to light up. I’ve restarted the BBB thinking this would ultimately reset the pins with no luck. I’ve also created a cleanup script to run in case I exit the program early without the cleanup function being called. Anyway as you can imagine this gets frustrating when going to everything working fine to nothing working at all. Any advice or troubleshooting suggestions would be appreciated. Thanks!

    1. Make sure your circuit is connected correctly. Test and see if the LED will light with a different pin. Go back and get the simple circuit and program working.

      1. Yup, I’ve deduced that the circuit is not the problem. This happens when I don’t change the circuit from a working example.

Comments are closed.