Archive

Archive for the ‘BeagleBone’ Category

BeagleBone – Stepping Out

April 3, 2012 2 comments

A stepper motor is an electro mechanical device which converts electrical pulses into discrete mechanical movements. The shaft or spindle of a stepper motor rotates in discrete step increments when electrical pulses are applied to it in the proper sequence. The motors rotation has several direct relationships to these applied input pulses. The sequence of the applied pulses is directly related to the direction of motor shafts rotation. The speed of the motor shafts rotation is directly related to the frequency of the input pulses and the length of rotation is directly related to the number of input pulses applied.

That’s quite the explanation. Most anything that requires motion control will use a step motor. So how do we get a BeagleBone to work a step motor?  We need four things:

  1. Step Motor – The motor steps (rotates) when the power is applied to the motor’s windings.
  2. Driver – The driver receives a command, interprets it, and send the necessary power to the step motor to achieve a step.
  3. Controller- A device (microprocessor) capable of sending a command to the driver.
  4. User Interface (UI) – A physical or virtual device that provides a user with effective interaction and control of the step motor.
Stepper Motor Driver Board ULN2003 for Arduino/AVR/ARM

Stepper Motor Driver Board ULN2003 for Arduino/AVR/ARM

There are all sorts of variations when it comes to integrating these four pieces. For example you could run a UI on a BeagleBoard that communicates, using one of a number of methods (i.e. USB, Ethernet), with a dedicated controller. That controller is cabled to a driver which in turn is wired to a motor. All well and good but I wanted a simpler and cheaper example.

In this example the BeagleBone will provide the UI and controller. The initial UI will be a shell script. While not very interactive it’s demonstrates that everything works. I’ll get more sophisticated later. The BeagleBone’s GPIO interface will be used to send commands to the driver.

I picked up an inexpensive driver and step motor intended for use with an Arduino. The driver board accepts a four bit command from an controller and in turn applies the necessary power pulse to step the motor. At the heart of the driver is a ULN2003AN integrated circuit. The board can supply between 5V to 12V to the motor from an independent power supply. It also has a bank of LED’s that correspond to the input signals received from the controller. They provide a nice visual when stepping.

Some step motor details:

  • Model: 28KYJ-48
  • Voltage: 5VDC
  • Phase: 4
  • Step Angle: 5.625° (1/64)
  • Reduction ratio: 1/64
It takes 4096 steps to rotate the spindle 360°. It is impossible to see a single step. When testing it pays to have something distinct on the spindle to show it is turning.

Physically connecting BeagleBone to the driver board is straight forward. Pick a free GPIO pin on the expansion header and run a wire from it to one of the input pins on the driver board. The driver board requires power. I tried powering it from the 5V and ground pins on expansion header P9, and an independent 5V power supply. Each worked well. Having wired a GPIO pin to the driver board you can use Linux’s sysfs GPIO interface to use it. Set the GPIO pin high and the corresponding LED on the driver board will illuminate. Set it low and the LED turns off.

Completing the wiring and toggling the LED’s did take long. Here’s now I wired the BeagleBone to the driver.

BeagleBone Drive Board
Signal Nane Pin Pin
VDD_5V  P9.3 +
GND  P9.1
GPIO1_6 p8.3 IN1
GPIO0_27 p8.17 IN2
GPIO1_15 p8.15 IN3
GPIO1_13 p8.11 IN4

The motor steps when a specific combination of wires running to the motor are powered. This is just a pulse of power. Just enough to get the motor to step. This driver and motor use a very simple protocol. Applying a signal to an input pin causes power to be sent to the motor on a corresponding wire.

Stepper Motor Driver Board ULN2003 for Arduino/AVR/ARM

Stepper Motor Driver Board

MCU IO Code Wire Color
IN1  A Blue
IN2  B Pink
IN3  C Yellow
IN4  D Orange

The motor steps when specific combinations of its wires are powered and the same combination is used to signal the driver. Discovering the correct stepping commands took a lot of searching on the Internet. The vast majority of information describes Arduino examples that rely on underlying Arduino libraries. I finally found this link and within a few minutes my motor was stepping.

The following codes define the step commands.

8 Step : A – AB – B – BC – C – CD – D – DA
4 Step : AB – BC – CD – DA (Usual application)

Step Command IN4 IN3 IN2 IN1
A 01H 0 0 0 1
AB 03H 0 0 1 1
B 02H 0 0 1 0
BC 06H 0 1 1 0
C 04H 0 1 0 0
CD 0CH 1 1 0 0
D 08H 1 0 0 0
DA 09H 1 0 0 1

Here’s a shell script that will step my motor one full rotation.

#!/bin/sh

timeout=10000

in1=38 # GPIO1_6  (p8.3)
in2=27 # GPIO0_27 (p8.17)
in3=47 # GPIO1_15 (p8.15)
in4=45 # GPIO1_13 (p8.11)

AB() {
        echo high > /sys/class/gpio/gpio$in1/direction
        echo high > /sys/class/gpio/gpio$in2/direction
        echo low > /sys/class/gpio/gpio$in3/direction
        echo low > /sys/class/gpio/gpio$in4/direction
}
BC() {
        echo low > /sys/class/gpio/gpio$in1/direction
        echo high > /sys/class/gpio/gpio$in2/direction
        echo high > /sys/class/gpio/gpio$in3/direction
        echo low > /sys/class/gpio/gpio$in4/direction
}

CD() {
        echo low > /sys/class/gpio/gpio$in1/direction
        echo low > /sys/class/gpio/gpio$in2/direction
        echo high > /sys/class/gpio/gpio$in3/direction
        echo high > /sys/class/gpio/gpio$in4/direction
}

DA() {
        echo high > /sys/class/gpio/gpio$in1/direction
        echo low > /sys/class/gpio/gpio$in2/direction
        echo low > /sys/class/gpio/gpio$in3/direction
        echo high > /sys/class/gpio/gpio$in4/direction
}

off() {
        echo low > /sys/class/gpio/gpio$in1/direction
        echo low > /sys/class/gpio/gpio$in2/direction
        echo low > /sys/class/gpio/gpio$in3/direction
        echo low > /sys/class/gpio/gpio$in4/direction
}

test() {
        for input in $in1 $in2 $in3 $in4
        do
                echo high > /sys/class/gpio/gpio$input/direction
                sleep 1
                echo low > /sys/class/gpio/gpio$input/direction
        done
}

step=0

while [ $step -lt 4096 ]
do
        echo "step: $step"

        AB
        usleep $timeout

        BC
        usleep $timeout

        CD
        usleep $timeout

        DA
        usleep $timeout

        step=`expr $step + 8`
done

off

Let’s see it in action.

Categories: BeagleBone Tags:

BeagleBone – How hot is it?

April 1, 2012 21 comments

TMP36 Temperature Sensor

I picked up a TMP36 – Analog Temperature sensor from adafruit. Time to see how to connect it to a my BeagleBone.

This sensor provides a voltage output that is linearly proportional to the Celsius (centigrade) temperature.

The TMP36 is specified from −40°C to +125°C, provides a 750 mV output at 25°C, and operates to 125°C from a single 2.7 V supply.

You calculate the temperature with the following forumula:

Temperature = ((Vout in mV) – 500mV) / 10.

I can use one of the BeagleBone’s analog to digital (ADC) interfaces to read the ouput from the TMP36. Things to get a little complicated because the ADC inputs are only 1.8V interfaces and the TMP36 is a 2.7V interface. If the temperature got hot enough the TMP36 could push enough voltage to potentially fry the ADC input. Since things won’t get that hot I’ll not worry about it. My tests won’t get an output from the TMP36 anywhere near 1.8V.

The TMP36 will be powered by pin 3 (3.3V) on expansion header P9. Ground can go to any of the ground pins on the expansion headers. I’ll use pin 1 on expansion header P8 for ground. The analog output will go to ADC interface AIN1 – pin 40 on expansion header P9. The BeagleBone’s ADC interfaces return a 12 bit value which will range from 0 to 4095.

BeagleBone with Temperature Sensor

BeagleBone with Temperature Sensor

Programming will again use the Linux’s sysfs. /sys/devices/platform/tsc provides access to the ADC interfaces. There are eight AIN files representing the eight ADC interfaces. To make things interesting their number scheme starts at 1 where as the symbolic name in the BeagleBone manual start at 0.

SIGNAL
NAME
AIN File Name
AIN0 /sys/devices/platform/tsc/ain1
AIN1 /sys/devices/platform/tsc/ain2
AIN2 /sys/devices/platform/tsc/ain3
AIN3 /sys/devices/platform/tsc/ain4
AIN4 /sys/devices/platform/tsc/ain5
AIN5 /sys/devices/platform/tsc/ain6
AIN6 /sys/devices/platform/tsc/ain7
AIN7 /sys/devices/platform/tsc/ain8

I can easily read the digital value of AIN1 from the shell using:

#cat /sys/devices/platform/tsc/ain2
1670

In this case I got back a value of 1670. Using a simple formula I can convert that value to the temperature.

Step Description Example
1 Read the digital value from the ADC interface. #cat /sys/devices/platform/tsc/ain2
1670
2 Convert the digital value to millivolts.
(value / 4096) * 1800mV
(1670 / 4096) * 1800mV = 733.8867mV
3 Convert the millivolts to Celsius temperature.
(millivolts – 500mV) / 10
(733.8867mV – 500mv) / 10 = 23.38867°C
4 Convert Celsius to Fahrenheit.
(Celsius * 9.0 / 5.0) + 32.0
(23.38867°C * 9 / 5) + 32 = 74.09961°F

Time for some C code.

#include
#include

double CtoF(double c) {
        return (c * 9.0 / 5.0) + 32.0;
}

double temperature(char *string) {
        int value = atoi(string);
        double millivolts = (value / 4096.0) * 1800;
        double temperature = (millivolts - 500.0) / 10.0;
        return temperature;
}

void main() {
        int fd = open("/sys/devices/platform/tsc/ain2", O_RDONLY);

        while (1) {
                char buffer[1024];
                int ret = read(fd, buffer, sizeof(buffer));
                if (ret != -1) {
                        buffer[ret] = NULL;
                        double celsius = temperature(buffer);
                        double fahrenheit = CtoF(celsius);
                        printf("digital value: %s  celsius: %f  fahrenheit: %f\n", buffer, celsius, fahrenheit);
                        lseek(fd, 0, 0);
                }
                sleep(1);
        }

        close(fd);
}

This program will loop indefinitely. Once a second it will read the/sys/devices/platform/tsc/ain2 which is the ADC (AIN1) interface connected to the TMP36. If it gets back a value the program will then calculate the temperature and print it out. It’s all pretty straight forward. A couple of notable things:

  • The string returned by the read is not null terminated.
  • After a read the file descriptor needs to be rewound to the start of the file using lseek.
Here’s some sample output.


digital value: 1658 celsius: 22.861328 fahrenheit: 73.150391
digital value: 1655 celsius: 22.729492 fahrenheit: 72.913086
digital value: 1661 celsius: 22.993164 fahrenheit: 73.387695
digital value: 1667 celsius: 23.256836 fahrenheit: 73.862305
digital value: 1657 celsius: 22.817383 fahrenheit: 73.071289
digital value: 1657 celsius: 22.817383 fahrenheit: 73.071289
digital value: 1663 celsius: 23.081055 fahrenheit: 73.545898
digital value: 1661 celsius: 22.993164 fahrenheit: 73.387695
digital value: 1657 celsius: 22.817383 fahrenheit: 73.071289

Categories: BeagleBone Tags:

BeagleBone GPIO Programming

March 31, 2012 3 comments

According to the BeagleBone System Resource Manual (SRM) the board has:

A maximum of 66 GPIO pins are accessible from the expansion header. All of these pins are 3.3V and can be configured as inputs or outputs. Any GPIO can be used as an interrupt and is limited to two interrupts per GPIO Bank for a maximum of eight pins as interrupts.

These pins are distributed across both expansion headers (P8 & P9) and their locations are well documented in the SRM. As described in a previous article, Linux provides a virtual file system called sysfs as a programming interface to system resources such as GPIO. The blink.js script that’s supplied with BeagleBoard’s Ångström’s Linux distribution will blink the User 3 LED and toggle GPIO1_6, which is located on the third pin of expansion header P8, high and low.

This article describes my experience getting an LED to blink using GPIO1_6.

Before I get into my LED project I want to note that I’m a typical software guy. I understand basic circuitry but my knowledge of electronics rapidly diminishes after that. So there are aspects of this journey that I’m discovering for the first time.  Most BeagleBone articles don’t detail the circuitry nor explain the electronics behind it. They assume the audience knows that stuff and focus on the BeagleBone part of things. I’m going to get into those details because I had to learn them before I could get this simple example to work.

LED Circuit

LED Circuit

An LED circuit is about a simple as it gets but still a bit of a learning experience for me.

  1. There are different kinds of LED’s. I picked up a standard red diffused 1 3/4 sized LED.
  2. The resistor is really important. Forget the resistor and you’ve created a short circuit. Not only will be burn out the resistor and your fingers it can damage the BeagleBone. The type of resistor you’ll need will depend on things like voltage and LED type. The resistor can be placed before or after the LED.
  3. An LED will only light with correct electrical polarity. You have to plug it in the right way.
GPIO Output to LED

GPIO Output to LED

I’m going to use GPIO1_6 as the power source for the LED circuit. When I set it high 3.3V will be supplied to the circuit and the LED will blink on. Setting GPIO1_6 low turns off the power the the LED will blink off.

Including a resistor in the circuit is a must. Knowing exactly what kind of resistor takes some testing. If you are in a hurry you can use 1k ohm (1/4 W – 5%) resistor. You’ll get a dim light but you’ll know it is safe.

Calculating the optimal type of resistor requires three values:

  • The source voltage. Since I’m using a GPIO pin it should be 3.3V.
  • The LED voltage. I used a multi-meter to get a value of 1.6V.
  • The LED current. Which after a lot of trial and error using a multi-meter comes in at 20mA.

Now to the formula.  The resistor value, R is given by: R = (VS – VL) / I

VS = supply voltage
VL = LED voltage
I = LED current

In my case this worked out to be 100ohm. There are all sorts of nifty calculators on the Internet. I used this one at LED Center to do the calculation for me.

This shows how I wired the circuit.

BeagleBone LED Circuit

BeagleBone LED Circuit

The red wire is plugged into GPIO1_6. That GPIO pin provides the power when it is set high. I’ve put a 100ohm resistor in front of the red LED. I could have put it after. You need wire the LED’s polarity correctly. If you wire it backwards the LED will not illuminate. The yellow wire runs to an expansion header GND pin and completes the circuit.

On the programming side we toggle GPIO1_6 on and off by using its sysfs interface. If I run blink.js both the User 3 LED and my red LED will blink. I wrote a shell script blink.sh to do the same thing.

#!/bin/sh

echo 38 > /sys/class/gpio/export

while :
do
  echo 1 > /sys/class/leds/beaglebone::usr3/brightness
  echo high > /sys/class/gpio/gpio38/direction
  sleep 1
  echo 0 > /sys/class/leds/beaglebone::usr3/brightness
  echo low > /sys/class/gpio/gpio38/direction
  sleep 1
done

/sys/class/gpio/gpio38 does not exist by default. You create it by writing the GPIO port number to /sys/class/gpio/export. You can remove it by writing the same number to /sys/class/gpio/unexport.

Here’s a video of it in action.

Categories: BeagleBone Tags: