Tag Archives: LED

Beaglebone Black LESSON 10: Dimable LED using Potentiometer

In this lesson we will create a dimable LED. We will read an analog voltage from a potentiometer, and use that to set the brightness on an LED. In order to proceed with this lesson, you will need to connect the following circuit:

Potentiometer Reading is Used to Set LED Brightness

Note we are using P9_32 as the reference voltage on the voltage divider, we are using P9_34 as the reference ground, and we are using P9_33 as the analog sense pin. We also using P9_14 as the PWM output pin. Note the current limiting resistor in series with the LED is 330 Ohm.

The object of this circuit is to read the value of the potentiometer and then to use that to set the brightness on the LED. We know that the value we read from the potentiometer will be between 0 and 1. We know that what we can control on the PWM pin is the duty cycle of the 3.3 volt signal. We know that when the potentiometer reads 0, we want a 0% duty cycle on the PWM pin, which would have the LED off. This is our first point:

(0,0)

We also know that when we read 1 from the potentiometer, we want to apply a duty cycle of 100%, or have the LED be full bright. This is our second point:

(1,100)

If we created an equation for the line between these two points, we could calculate the duty cycle that should be applied based on the potentiometer reading. The problem with this is the way our eye perceives changes in brightness. We perceive exponential changes, so if we connected the two points with a linear relationship we would see lots of change at the low end of the scale, but as we continued to move the potentiometer, the brightness would appear to saturate. In order to have a nice smooth transition from full dim to full bright as the potentiometer is moved from left to right, we need to fit an exponential curve between the two points above. We want the LED to be off when the pot is full left, and full bright when the pot is fully to the right. We could use the following exponential equation:

Duty Cycle = C^(Analog Read) – B

This should do the trick, but we need to figure out what the constants C and B need to be. We do this by first plugging in the first point (0,0) from above:

0 = C^0 – B

Anything raised to 0 power is 1, so we have:

0= 1 – B

So B = 1. There, we have our first constant. We use this, and our second point to find C.

100 = C^1 – 1

101=C^1

C=101

Now we have everything we need to calculate the Duty Cycle from the value we read from the potentiometer. The final equation is:

Duty Cycle = 101^(Analog Read) – 1

This Chart Shows how to Map Duty Cycle onto Analog Read

Note that this relationship has the desired properties. When we read a 0 from the potentiometer, we apply a Duty Cycle of 0% to the PWM pin, and the LED is off. When we read a 1 from the potentiometer, we apply a Duty Cycle of 100% to the LED and it is full bright. The exponential shape of the curve between these two points ensures that we will perceive a smooth increase in brightness as we turn the potentiometer up. Math works! It would be very hard to do this by trial and error.

We are now ready to begin developing our code. The video lesson explains the code line-by-line, and we are using commands we learned in the last few lessons.

The code works very well, and produces a very smooth transition from fully off to fully bright.

Beaglebone Black LESSON 7: Create a Dimable LED Circuit with PWM in Python

In this lesson we will explore how to use the PWM commands we learned in the last lesson to control the brightness of LEDs in a circuit. (If you do not have a Beaglebone Black yet, you can pick one up HERE.) In order to proceed with this project, you should hook the following circuit up to your Beaglebone Black.

This Circuit is Used to Demonstrate a Dimable LED Project

Note that we are using pins “P9_14” and “P9_22” which are good working PWM pins. Note the current limiting resistors are 330 ohm. Always connect the long leg of the LED towards the control voltage.

The video steps you through the code to control the LED brightness.

Beaglebone Black LESSON 5: Blinking LEDs from GPIO Pins

This lesson shows a simple example of how to blink two LEDs from the GPIO pins on the Beaglebone Black. To get going, you will need to hook up the following circuit. (If you have not ordered your Beaglebone Black, you can get one HERE.)

Circuit for Blinking LEDs from Beaglebone Black

Note that the Top LED is connected to Pin “P9_12” and the bottom LED is connected to Pin “P9_11”. We are using 330 ohm current limiting resistors.

The video lesson takes you through several examples of how to blink the LED. Watch the video, and do the examples. Then play around on your own and see what you can make the LEDs do.

Raspberry Pi LESSON 31: Making a Dimable LED with Python

In this lesson we are ready to bring together a lot of what we learned in earlier lessons. We will create dimable LEDs which will respond to two buttons. If one is pressed the LED will gradually grow dimmer. If the other is pressed, the LED will gradually grow brighter. This will require us to use our skills in using GPIO inputs, pullup resistors, GPIO outputs, and PWM.

For convenience we will use the same circuit we used in LESSON 30, shown below. Also, if you want to follow along with these lessons, you can buy the gear you need HERE.

This Circuit Controls two LED from Push Buttons Using the Raspberry Pi

The objective of this circuit is that we want the LEDs to grow brighter each time the right button is pushed, and we want them to grow dimmer each time to left button is pushed.

The video above steps through and explains this code.

 

Raspberry Pi LESSON 30: Controlling LEDs from Push Buttons

In this lesson we will show how you can control LED’s from push buttons. In order to get started, you will want to expand the circuit we built in LESSON 29 to include two LEDs. The schematic below shows how you will want to hook things up (Also, remember you can see the Raspberry Pi pinout in LESSON 25). Also, as we have mentioned before, if you want to follow along with us in these lessons you can get a kit that has all the gear you need HERE.

This Circuit Controls two LED from Push Buttons Using the Raspberry Pi

In the video lesson, we take you through the code step-by-step. We use the techniques learned in LESSON 29 to detect if a button has been pushed. We introduce two new variables, BS1 and BS2, so indicate the state of the LED’s. A BS1=False means the LED1 is off. A BS1=True means the LED is on. This concept allows us to determine whether we should turn the LED on or off when the button is pushed. Basically, we want to put it in the opposite state when a button is pushed. The code is below. The video shows how it works.