Tag Archives: Beaglebone Black

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.

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

B=1

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.

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 9: Reading Analog Inputs from Python

If you went through our series of lesson on the Raspberry Pi, you will remember that we found the major limitation of the Pi is that it has no analog input pins. Luckily, the Beaglebone Black as a number of analog input pins, so we can greatly expand the scope of projects we can do.  The pinout below shows the pins that are available on the Beaglebone Black for analog input. (If you do not already have your Beaglebone Black, you can pick one up HERE.)

Default Pin Configuration for the Beaglebone Black Rev. C.

You can see the blue shaded pins in the diagram above are for analog input.

A couple of very important points. These pins are designed to read analog voltages between 0 and 1.8 volts. Applying voltages above 1.8 volts can burn out the pin, or even smoke the Beaglebone. Hence, as you set up voltage divider circuits you must ensure they have a rail of 1.8 Volts, to ensure that the analog in pins will never see more than 1.8 Volts. Luckily, the Beaglebone provides a handy 1.8 Volt reference signal on pin 32  (on P9 header). Always use pin 32 as your reference rail when working with analog inputs. Similarly, you should use pin 34 (on P9 header) as your reference ground on your analog input circuits.

To demonstrate how to do analog reads, we will set up a simple voltage divider using a potentiometer. Go ahead and hook up your circuit as follows:

A Simple Voltage Divider Using a Potentiometer

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.

With this circuit hooked up we are ready to develop some code. In the attached video we take you through this program step-by-step to show you how you can make analog readings from the potentiometer using python.

 Note the analog read returns a number between 0 and 1, which is proportional to the applied voltage. Hence to convert to actual voltage, we multiply this read value by 1.8 Volts.

Beaglebone LESSON 8: Read Button State from Python


In this tutorial we will see how to read digital values from the GPIO pins. We will be doing digital reads, which means we will be limited to “HIGH” or “LOW” readings. This is a 3.3 volt system, so we need to make sure that the “HIGH” applied signal is 3.3 volts.

Our pinout from LESSON 1 shows which pins are suitable for digital reads.

Default Pin Configuration for the Beaglebone Black Rev. C.

It is the green GPIO pins which we can use for digital reads. In this lesson we will demonstrate the digital read technique using a simple two button circuit. In order to complete this lesson, you should go ahead and build this circuit.

Example of Simple Beaglebone Black Button Circuit

Notice we are using Pin 1 on Header P9 as the ground and Pins 11 and 13 on header P9 s the input pins. Also note the pulldown resistors are 1000 Ohm. It is important to use at least this much resistance. If you do not have 1,000 Ohm resistors, using something larger NOT something smaller.

Once you have the circuit set up we are ready to begin programming.

First up, you need to import the GPIO library. If you have the latest version of Debian Wheezy, you should have the library on your system. If you do not have it you will need to update and upgrade your operating system. To load the library, you will use the python command:

We now need to configure out pins P9_11 and P9_13 as inputs. We do this with the command:

Now to read the state of the buttons, we would use the commands:

state1 will be True if the top button is pushed, and False if it is not being pushed. Similarly, state2 will be True when the button is being pushed, and False when it is not being pushed.

We can bring these concepts together to make the following program. Play around with the program and see what all you can make it do.

 

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.