# Beaglebone Black GPS LESSON 1: Hooking Up the Adafruit Ultimate GPS

If you went through our series of 12 lessons on the Beaglebone black you should be familiar with the basics of this microcontroller. We are now ready to move on to more advanced projects. In our earlier lessons on the Arduino, we built a GPS data logger and integrated it with Google Earth using the Arduino Uno and the Adafruit Ultimate GPS Breakout Board. While that was a great project, we finally ran out of horsepower with the arduino, and what we could do was limited by the memory limitations on the Arduino. Also, it is very hard to parse strings in the Arduino IDE, so interpreting the NMEA sentences is a rather large challenge with the limited string functionality in Arduino.

Python on the other hand makes quick work of string manipulation and the Beaglebone Black has plenty of horsepower for any manipulation of the NMEA sentences we might want to do.

In order to play along with this project, you will need a Beaglebone Black Rev C, which you can get HERE, and the Adafruit Ultimate GPS which you can get HERE. The video below takes you through the project step-by-step, as well as the description below.

Once you get your gear, you will want to hook up the following circuit:

Note that we are working off header P9 and we are using P9_1 as ground, P9_7 as VIN, we are using P9_24 as  Tx and P9_26 as Rx. Please note that you can see a detailed Diagram of the Beaglebone Black pinout HERE. Also notice that Tx on the Beaglebone is connected to Rx on the GPS, and Rx on the Beaglebone is connected to Tx on the GPS. Tx is like “talk” and Rx is like “listen, so you want to listen to the pin that is talking, and you want to talk to the pin that is listening.

Our goal in this first lesson is to establish a connection between the GPS and the Beaglebone, and to read in the data streaming from the GPS. We want to get a fix, and verify that we can read and print the NMEA sentences that contain the various position, altitude and velocity data.

The video takes you step-by-step through the code. The following simple code will get you streaming data from the GPS to your terminal window. In future lessons we will break the data down and show you how to get your position from the raw NMEA sentences streaming in.

# Beaglebone Black LESSON 12: Control a Servo from Python Using PWM

This lesson will show how to use Python running on the Beaglebone Black to control the position of a servo. First, I am using a small servo that I have verified can be powered from the Beaglebone Black without drawing too much current. All servos are different, so if you are unsure of the current requirements of your servo, it is safest to power it from an external 5 Volt power source. You can still control it through the control line connected to the Beaglebone Black, just make sure the servo and Beaglebone have a common ground.

Most all servos have three wires; Power, Control, and Ground. For my servo, Control is Yellow, Power is Red, and Ground is Black. If you have a different servo, check the data sheet to see what colors are Control, Power and Ground on your servo. Note we are using pin P9_2 as ground, P9_7 as 5V power, and P9_14 as our control pin.

We will be controlling the position of the servo using PWM. We will have to play around with our individual servo to determine precisely what signal pulse width will result in the servo being in the full left position, and what pulse width will result in the servo being in the full right position.

For most servos, full left is somewhere around 1 milliseconds, and the pulse width that will give us full right position is about 2 milliseconds. These are ballpark numbers, and we will have to play around with things to find the exact values for our servo.

We will set up a 50 Hz PWM signal. A 50 Hz signal has a period of:

period = 1/frequency = .02 seconds = 20 miliseconds.

Hence, if we want to get to about the full left position we would want a duty cycle of about 5%. Because 20 milliseconds x .05 = 1 milliseconds. This one millisecond pulse width should put the servo about in the full left position. Similarly for the full right position, we would want a duty cycle of 10%, because that would give us a pulse width of 2 milliseconds, since:

PulseWidth = Period x DutyCycle

PulseWidth = 20 x .1 = 2 milliseconds.

We can use the following code to determine precisely for our servo what dutyCycle will give the precise full left and full right positions.

For my servo, running a 50 Hz PWM singnal, I find that a duty cycle of 2% puts it in the full left position and a duty cycle of 12% puts it in the full right position.

Now we would like to be able to just specify an angle we want and have it go to that angle. If we want an angle of 0 degrees we would apply a 2% duty cycle. This value is for my servo. You will have to play around with your servo and the code above to find what this number is for you. But for me, I have the point:

(0,2)

That is to say when I desire an angle of 0 on the servo, I should apply a dutyCycle of 2 to the PWM pin. Similarly, when I desire 180 degrees, I should apply a dutyCycle of 12. (Again, this number might vary for your servo). For my servo, I get the point:

(180, 12)

We can fit a line to these points, which would then allow us to calculate the dutyCycle for any desired angle. The slope from the two points above would be:

m=(y2-y1)/(x2-x1)=(12-2)/(180-0) = 10/180= 1/18

Using the point slope form of the line, we would get

y-y1 = m (x- x1)

y – 2 = 1/18( x – 0)

y= 1/18*x + 2

Now putting in our actual variable names we get:

dutyCycle = 1/18*desiredAngle + 2

You can develop the same type equation just using the values suitable for your servo from the experiment above.

Now we can use this code to smoothly move the servo to any desired position.

# 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:

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

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.

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.)