Raspberry Pi and L298N motor driver boards

L298 Driver board

A few connections to the Raspberry Pi, a power supply and motors… simple!

A quick search of Ebay revealed these little beauties – L298 motor drivers. There’s a 3-pin terminal block for power (I used the 12v and Ground connection), 4 pins available for connecting to the GPIO and terminal blocks for a pair of DC motors. I didn’t use the 5v connection.

At the end of the 4 pins you will also find the “enable” inputs. This could be used to control the motors from a common PWM signal but I left the jumper links in place so that the motor controllers are always active.

In order to connect the driver board to the Raspberry Pi, a 4-way female-to-female ribbon connector is needed. Also required is a common ground connection and a simple way of doing this is to cut the end off a spare connector cable, strip it and push it into the ground connector for the power supply.

The module specifications state that it can work from a wide range of power supplies(5-35V) ¬†and interfaces easily with the Raspberry Pi. I’ve so far used it with the 9v Fischertechnik motors but my next stage is to connect it to some 4.5v Lego Technic motors. I’ve done similar with the L293 and the PICAXE so I’m not expecting any difference. I haven’t yet found out what the voltage drop across the internal transistors is, so I’ll have to pick my power supply with care.

L298 diagram

These are the basic connections. I left the three jumpers as supplied.

Another use that might not be so obvious is that the module can also drive light bulbs and stepper motors. One board would suit a 2 phase bipolar motor and it would be a straightforward job to write the patterns required to step the motor around. Note, however that this would require four GPIO connections per motor, so it might be convenient to use some sort of port expander if this is the direction chosen.

This particular driver board will probably end up on my disassembled Big Trak. I’ve got a 5v power supply that will run from 7.2v R/C racing packs.


i2c LCD and Digital Temperature Sensor

DS18B20 temperature sensor on breadboard

DS18B20 digital temperature sensor – only three wires and a resistor needed!

These DS18B20 devices are pretty nifty. I remember all of the hassle connecting an LM35 to a computer, sorting out the changes in current into something a BBC analogue port can handle, calibrating and scaling. And now… One little device, a 4k7 resistor and a few lines of code.

Here I’ve combined the sensor and the i2c display together to give me the Pi temperature and the room temperature. It’s been quite mild this autumn even without the central heating on!

There are a number of tutorials on the internet (such as this one)showing how these devices are used. I’m more familiar with using them with a PICAXE but they’re just as simple with the Raspberry Pi. It’s important to note that each of the sensors has their own address, so make sure you check carefully.

The code is straightforward:

import os
import commands
import time
import pylcdlib
import datetime

lcd = pylcdlib.lcd(0x27,0)
lcd.lcd_puts("                 ",1) #display on line 1
lcd.lcd_puts("                 ",2) #display on line 2

def get_cpu_temp():
	tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
	cpu_temp = tempFile.read()
	return float(cpu_temp)/1000

while True:
	tfile = open("/sys/bus/w1/devices/28-0000011628bf/w1_slave")
	text = tfile.read()
	secondline = text.split("\n")[1]
	temperaturedata = secondline.split(" ")[9]
	floattemperature = (float(secondline[29: ])/1000)
	lcd.lcd_puts("Temp: "+str(floattemperature),2) #display on line 2

	lcd.lcd_puts("CPU :"+str(get_cpu_temp())+"       ",1) #display on line 1

The LCD is really hard to photograph. I’ve tried a whole number of settings on the point’n’squirt camera at home, as well as my phone, but neither really shows up how well these LCDs show text.

Having used the LCDs with the PICAXE, I’m sure it’s possible to access the degrees symbol! Another challenge is using i2c with Python3. It seems that the libraries are unavailable, although trawling the ‘net has shown some leads that I’ll follow up.

Display showing the temperature.

LCD showing processor temperature and the temperature returned by the DS18B20.

Where next? My earliest experiments involved using the sensor with a PICAXE, which then sent the data to the Raspberry Pi as a serial data stream. These results were then uploaded to COSM (now known as Xively, having had a brief spell as Pachube). It was interesting to watch COSM plot the graphs over a period of time. It wouldn’t take much imagination to see how this could easily become an environmental monitoring and control platform – and many have succeeded!

Fischertechnik, Scratch and Motor Control through the GPIO.

Fischertechnik robot arm connected to two L298 driver boards and controlled through Scratch.

Fischertechnik robot arm connected to two L298 driver boards and controlled through Scratch.

My greatest interest in the Raspberry Pi lies in those 26 little pins tucked down on the side of the board. They just invite things to be connected to them. Having found a pile of dusty Fischertechnik (loosely assembled in a sort-of robot arm shape) in a cupboard, I decided that something could be done with it.
The arm was so dusty it needed disassembling and washing. I left the motors and sensors out of the wash but I figured that everything else would cope. Sure enough, it looked like new again.

I found some L298 motor driver boards on Ebay and these easily connect to the Raspberry Pi GPIO connections. There are four motors in this arm:

  • Rotation of the base
  • Lifting of the “forearm”
  • Lifting the “upper arm”
  • Closing the jaws

Continue reading

Minecraft on the Raspberry Pi

mysnapshot_04Well… I escaped the whole Minecraft phenomenon for this long. Sure, I’d heard about it from the students and a few visitors at the Exeter Raspberry Jam loaded it up on my Pi, but I’d never delved any further.

Minecraft appears to be a game which puts the user in control of “Steve” – a superhero who can hop and fly at the touch of the spacebar. Armed with his trusty sword which can build and tear down (echoes of Ecclesiastes 3) he can navigate the cubic world at will. The world coordinates are limited to +/- 127 units but that’s a fair amount of walking and flying. I’m guessing the scale is somewhere around 1 unit = 1 metre. It’s all vaguely reminiscent of 3d Construction Kit which I used to run on my Amiga. Continue reading