Jeroen Swart

.NET Architect

Netduino - Getting Started with LCD

If you want to show more output then blinking LED’s, an LCD module is a good and usually affordable option. They come in different sizes, from 2 numeric-only digits to 40x4 alphanumeric dot-matrix modules. You can also get graphic LCD’s, that range from modules with small resolutions (e.g. 122x32 or 240x128 pixels) to more professional LCD screens (e.g. 5” or 7”),  but I will stick with alphanumeric LCD’s for this post.

Most alphanumeric LCD modules are built around a HD44780 or compatible controller, so they all use the same interface. They are controlled using an 8-bit parallel port, some pins for handshaking (controlling the transfer of data over the parallel port), pins for controlling the contrast and backlight if available and of course pins for power and ground. The parallel port can also be used in 4-bits mode, sending the 8-bit data in two write actions. This saves I/O ports, but takes more time to update the LCD.

There is an excellent library available on codeplex (with a good introduction on the blog of the author) for controlling LCD modules. This library makes showing text on a display as easy as calling a Write method with the desired string. It uses a provider-pattern to allow for different connection scenarios, like direct on GPIO pins, using a shift-register or using an I2C expander. And if you want to use another scenario, you can add your own provider.

I have a 20x2 LCD module that I connected directly to the GPIO pins of my Netduino using the parallel port in 4-bit mode. The connections are made on a breadboard, because this makes it a little easier to put all the wires together. Below is an image of the result. The sample code provided with the library contains a link to a very helpful schematic on the Arduino website. Remember to connect the power on the LCD module to the 5V of the Netduino. 3.3V is not enough and will leave the LCD blank.

 The library contains a simple example of how to use the library. It initializes the LCD, shows the obligatory “Hello world” and then enters a loop that shows the milliseconds since reset. Since I use the GPIO pins directly, I changed the provider in the example. The code I used is shown below.

public class Program
{
    public static void Main()
    {
        // create the transfer provider, use direct GPIO provider
        // Initialize the library with the numbers of the interface pins
        // Use wiring shown here http://arduino.cc/en/uploads/Tutorial/lcd_schem.png
        var lcdProvider = new GpioLcdTransferProvider(Pins.GPIO_PIN_D12,    // RS
                                                        Pins.GPIO_PIN_D11,    // ENABLE
                                                        Pins.GPIO_PIN_D5,     // D4
                                                        Pins.GPIO_PIN_D4,     // D5
                                                        Pins.GPIO_PIN_D3,     // D6
                                                        Pins.GPIO_PIN_D2);    // D7

        // create the LCD interface
        var lcd = new Lcd(lcdProvider);

        // set up the LCD's number of columns and rows: 
        lcd.Begin(20, 2);

        // print a message to the LCD.
        lcd.Write("Hello world!");

        while (true)
        {
            // set the cursor to the first column on the second line
            lcd.SetCursorPosition(0, 1);

            // print the number of milliseconds since reset:
            lcd.Write((Utility.GetMachineTime().Ticks / 10000).ToString());

            Thread.Sleep(100);
        }
    }
}

 

Netduino - Getting Started with steppermotors, shieldless

In my previous post, I explained how I used the Adafruit motorshield to control a steppermotor. Since this shield uses a shift register, it needs a lot of I/O-actions. To control the steppermotor without a shift register, I put together part of the motorshield schematic on a breadboard. What remains is a single L293D with a few supporting parts, as shown in the schematic below.

You can put the schematic together on a small breadboard. Connect D0, D1, D2 and D3 to the corresponding ports on the Netduino. Connect all grounds to the ground on the Netduino, all the VCC to the Netduino 3.3V and the Vin to the Vin of the Netduino. The power of the adapter will be on Vin, powering the motor. The image below shows my result.

I changed the library for the motorshield, used in my previous post, to work with the schematic above. You can download it here. The library only supports steppermotors and it only provides support for single, double and interleave step-types. Pins D0 - D3 are used by default, but you can use any of the digital I/O pins by providing them to the constructor of the Stepper class.

The sample program that comes with the library download does the same as the sample program in my previous post: it initializes a stepper motor with 200 steps per revolution (or 1.8 degrees per step) and then repeats a few forward and reverse 360 degree rotations for each supported step-type. An example of initializing the motor and moving it forward is shown below.

Stepper stepper = new Stepper(StepperPorts.M3_M4, 200);
stepper.Step(200, MotorDirection.Forward, StepType.Single);

But… does it increase the speed of the motor? Well, yes and no. For one motor it does, and for another it doesn’t. The first motor I tested (200 steps per rotation; the one in the image above) runs at 1.36 rotations per second using the shield and at 2.46 rotations per second without the shield, so using the direct control almost doubles the speed. The second motor (a small one with 48 steps per rotation) runs at 5.68 rotations per second with the shield and at 4.04 rotations per second without it, so slowing the motor down by almost 30%.

Of course, the measurements are hardly scientific, and I only tested two motors. I ran each motor for 10 full rotations and recorded the time that took. I did this 10 times for each step-type and calculated the averages.

However, there are some issues with this approach at the moment. Running the step-loop at the highest speed doesn’t move either motor one bit or they just shake somewhat. I had to put in a minimum wait-time of 1 msec after each step to get the motors to rotate. Another problem is the decrease in torque. I need to take a good look at the pulses that come out of the output-pins. Each part of the H-bridge is now controlled one after the other, instead of all at the same time (by setting up all the right output levels first, then providing them to the H-bridge all at once). I want to have a look at this, as soon as I can get my hands on an oscilloscope :-).

It’s not really the result I was hoping for. I’m looking for a speed of about 50 rotations per second. I’m planning to use NEMA 34 motors, which can do that easily. I’m not yet sure what’s causing the issues, but I do know that controlling the pulses directly with the microcontroller will not get me the high speed I’m looking for. One solution could be the use of an L297 chip. This is a steppermotor controller that turns two signals, a clock pulse (e.g. generated with PWM) and a direction signal, into the correct pulses for the H-bridge.

Next: working with LCD.

Netduino - Getting Started with steppermotors

Working with steppermotors is a bit more complex then working with regular DC motors. Apply a voltage to a DC motor and it will rotate. With stepper motors, you need to apply that voltage in a specific pattern.

Instead of using a single coil, as in a DC motor, most steppermotors use two coils. The number of steps is determined by the number of magnetic poles on the rotor. Two common types of steppermotors are unipolar and bipolar steppermotors.

Bipolar & unipolar

A bipolar steppermotor is controlled by reversing the polarity of each coil in the right order. A more detailed description of how a bipolar steppermotor works can be found here.

In a unipolar steppermotor the two coils each have a wire connected to their center. It is controlled by providing power between the center and one of the ends. The center of the coils is always connected to ground. Applying a positive voltage to one or the other end of a coil has the same effect as reversing the voltage on a coil in a bipolar motor. A more detailed description of unipolar steppermotors can be found here.

Wikipedia provides a nice article on steppermotors, which also shows an animation of a steppermotor.

The picture below shows some steppermotors. These are all unipolar. The left and right motors are the same type.

Below are two more pictures of the insides of the steppermotor.

   

Hardware

Since it takes some power to control motors, microcontrollers normally can’t control motors directly. Transistors are up for this job, as I explained before. To be able to provide some extra power, darlington transistors are often used with motors instead of single transistors. A darlington transistor is a set of two transistors that function as one, but can provide a much higher current gain than each transistor by themselves. The image below shows the symbol of a darlington transistor. You can read more information about them on Wikipedia.

Steppermotors are controlled by applying voltage to the ends of each coil (the center taps of a unipolar steppermotor are connected to ground), so you’ll need four (darlington) transistors. Such a set of transistors is called an H-bridge. You can make an H-bridge yourself or you can use an H-bridge chip. There are a lot of H-bridge chips available, like the L293D (handles 600mA), the L298 (handles up to 4A) or the ULN2004 (handles 500mA) to name a few.

Of course, you can also pick one of the motor shields available from stores like Adafruit or Sparkfun. I started with the motorshield from Adafruit. It consists of two L293D’s and can control 2 steppermotors (or 4 DC motors). It uses a serial/parallel shift register (74HC595N) to control the L293D’s with the least amount of I/O pins from the microcontroller. This is a nifty trick, although it makes the software a little bit more complex. It also takes more time to control the L293D’s. Each bit of the shift register (8 in total) must be set on a single data-pin and each data-bit is moved into the shift register using a pulse from a clock-pin; finally the result is made available on the parallel outputs of the register using a latch-pin, resulting in a total of 25 I/O-actions. This does not have to be a bad thing, but it does take more time and therefore limits the maximum speed of the motor.

The image below shows the motorshield, stacked on a Netduino, with a stepper motor connected. Since the USB connection can’t provide the power, I'm using an external power adapter.

Software

Adafruit provides an Arduino library for the motorshield. I found a Micro .NET port of this library on the Netduino forums, but it wasn’t working properly. You can download my version here. The microstep step-type (see below) still isn’t working, my motor just wiggles. I don’t think I’ll need microstepping myself, so I’m not really in a hurry to get it working :-). If you do need it, let me know and I’ll be glad to help. I also haven’t tested the DC motor part of the library.

The library provides four types of step-behavior: single, double, interleave and microstep. The single step-type activates a single coil at a time; this is also referred to as ‘wave drive’. The double step-type activates both coils at the same time; also referred to as ‘full step drive’. Double stepping uses twice the power and provides more torque, although not twice the torque. The interleave step-type combines both the single and the double step-type, activating one coil, then two coils, then one coil again and so on; this is also referred to as ‘half stepping’. Since each activation only makes a half step, this step-type requires twice the amount of steps to make the same rotation as the single or double step-type. The microstep step-type uses PWM (explained here) to have a finer control over the power supplied to the coils. Instead of simply switching a coil on or off, microstepping provides a sine waveform to each coil. This provides smoother rotation, but reduces the accuracy of the steps. As I mentioned, it’s currently not working in the library.

The sample program in the solution initializes a stepper motor with 200 steps per revolution (or 1.8 degrees per step) and then repeats a few forward and reverse 360 degree rotations for each (working) step-type. Below is a short example of how to initialize a motor and move it forward.

Stepper stepper = new Stepper(StepperPorts.M3_M4, 200);
stepper.Step(200, MotorDirection.Forward, StepType.Single);

I feel the shift register is limiting the speed of the steppermotor too much. So next, I will try to control the H-bridge directly, without the shift register.

Next: working with steppermotors, without the shield.