Picaxe Examples
These examples are designed to demonstrate how to use our modules with the Picaxe. All the modules which use the I2C bus have 1k8 pull-up resistors to 5v. You only need one set of resistors, located near the PicAxe, regardless of however many I2C devices you have connected to it.
Not shown on the schematics, you should fit a 100nF capacitor between the 5v supply and ground (0v). This should be located as close to the Picaxe chip as possible.
Index:CMPS03 / Magnetic Compass
SRF01 / Ultrasonic Ranger
SRF02 / Ultrasonic Ranger
SRF04 / Ultrasonic Ranger
SRF05 / Ultrasonic Ranger
SRF08 / Ultrasonic Ranger
SRF10 / Ultrasonic Ranger
SRF235 / Ultrasonic Ranger
TPA81 / 8 Pixel Thermal Sensor
SD20 / Servo Controller
SD21 / Servo Controller
MD03 / 24V 20A Motor Driver
MD22 / Dual 24V 5A Motor Driver
RLY08 / Relay Module
CMPS03 Magnetic Compass
This uses the I2C bus to connect the Picaxe to the CMPS03. It reads the single byte bearing and displays the bearing as a number 0-255 on the PC.
main:
i2cslave $C0,i2cfast,i2cbyte ' Define i2c slave address for the CMPS03
bearing:
readi2c 0,(b0) ' Read CMPS03 Software Revision
readi2c 1,(b1) ' Read compass bearing as a byte (0-255 for full circle)
debug b0 ' Display Software Revision
debug b1 ' Display Bearing
goto bearing
SRF01 Ultrasonic Rangertwo PicAxe pins. A schottky diode, such as a BAT85, is used to prevent the SRF01 driving the Picaxe's output. You can have up to 16 SRF01'sconnected to the PicAxe. The Range is displayed in a debug window on the PC.
The SRF01 uses a single pin for both serial input and output, however because the Picaxe uses separate pins for input and output, we still need setfreq m8 ' sets frequency to 8Mhz to allow for greater baud rate /
pause 1000 ' Wait for srf01 to power up
main: low 5 ' Send pin low and then high again to send break
pause 10
high 5
pause 10
SEROUT 5,T9600_8,(1,84) ' Send serial command to get ranging
pause 10
SERIN 1,T9600_8,b1,b2 ' Read in 2 bytes
debug w0 ' Display them a 16bit word
goto main
SRF02, SRF08, SRF10, SRF235 Ultrasonic Rangers
The SRF02, SRF08, SRF10 and SRF235 all use the same I2C interface. The basic ranging commands are the same, so this example works for all these rangers.
i2cslave $E0,i2cfast,i2cbyte ' Join i2c with SRF02
main: i2cwrite 0,(81) ' Send command for ranging
pause 70 ' wait for ranging to complete
i2cread 2,(b1, b0) ' Read in high byte and low byte
debug w0 ' Display them in debug window
goto main
SRF04 Ultrasonic Ranger
To work with the PicAxe, the SRF04 requires its timing pin (marked "do not connect" on the tech page) connected to ground, as shown below.
'* Example code for SRF04/05 *
main: low 1 ' Set output pin low
pulsout 1,1 ' Send a pulse to start the ranging
pulsin 1,1,w1 ' Recieve timed pulse from SRF05/04
w3=w1*10 ' Calculate distance
w3=w3/58
debug w3
pause 10 ' Wait before next pulse
goto main
SRF05 Ultrasonic RangerAs the PicAxe uses separate pins for input and output, the SRF05's one pin mode is not usable on the PicAxe. This example uses the SRF05 in two pin mode, where the Trigger and Echo signals appear on the same pin. Note the SRF05's mode pin is unconnected to place it in two pin mode. The software for the SRF05 is identical to the SRF04 example above. /
'* Example code for SRF04/05 *
main: low 1 ' Set output pin low
pulsout 1,1 ' Send a pulse to start the ranging
pulsin 1,1,w1 ' Recieve timed pulse from SRF05/04
w3=w1*10 ' Calculate distance
w3=w3/58
debug w3
pause 10 ' Wait before next pulse
goto main
TPA81 Thermal SensorThe TPA81 connects to the Picaxe using the I2C bus. This example displays the ambient temperature and 8 temperatures from thermal sensor, on an LCD03 module. /
i2cslave $D0,i2cfast,i2cbyte ' Join i2c with TPA81
main: for b0 = 0 to 29 ' Start a loop that increases value b0
i2cwrite 0,(b0) ' Write value of b0 to command register, this will move the servo
gosub getTemp ' Goes to a sub routine that gets the temperature
pause 200
next b0
for b0 = 30 to 1 step -1 ' Start loop that decreases value of b0
i2cwrite 0,(b0) ' Write value of b0 to command register, this moves the servo
gosub getTemp ' Get the temperature
pause 200
next b0
goto main
getTemp: i2cread 2,(b2,b3,b4,b5,b6,b7,b8,b9) ; Read the 8 temperature bytes starting at address 2
debug ; print results to picaxe debug screen
return
SD20 Servo Controller
Although the Picaxe is capable of outputting servo pulses, it places restrictions on your software and uses up a lot of valuable I/O pins. By using the SD20 chip, you can control up to 20 servo's.
main:
i2cslave $C2,i2cfast,i2cbyte ' Define i2c slave address
wave:
writei2c 1,(1) ' Sends a value of 1 to first servo pin
pause 1000
writei2c 1,(254) ' Sends a value of 254 to second servo pin
pause 1000
goto wave
SD21 Servo Controller
The SD21 is a ready wired module which can save a lot of time compared to the SD20 above, and the Picaxe fit on a socket on the SD21. This example moves a servo through its maximum range.
main: i2cwrite 63,(255)' Set servo 1 to 255
pause 500
i2cwrite 63,(128)' Set servo 1 to 128
pause 500
i2cwrite 63,(0) ' Set servo 1 to 0
pause 500
i2cwrite 63,(128)' set servo 1 to 128
pause 500
goto main
MD03 24V 20A Motor Driver
This example runs the motor forwards and backwards.
/ symbol speed = b0 ' Variable that sets the speed of the motor
i2cslave $B0,i2cfast,i2cbyte ' Join i2c with MD03
main: for speed = 0 to 243 ' Start a loop that increments speed
i2cwrite 2,(speed) ' Writes value in speed to the speed register
i2cwrite 0,(1) ' Drives motor forward
pause 10
next speed
for speed = 0 to 243 ' Start a loop that increments speed
i2cwrite 2,(speed) ' Writes value in speed to the speed register
i2cwrite 0,(2) ' Drives motor reverse
pause 10
next speed
goto main
MD22 24V 5A Motor Driver
This example runs the motors forwards and backwards.
i2cslave $B0,i2cfast,i2cbyte ' Join i2c with MD22
i2cread 7,(b0)
debug b0
main: i2cwrite 0,(0) ' Set mode to 0
i2cwrite 1,(255) ' Drive left motor full forward
i2cwrite 2,(128) ' Stop right motor
pause 1000
i2cwrite 1,(128) ' Stop left motor
i2cwrite 2,(255) ' Drive right motor full forward
pause 1000
i2cwrite 1,(0) ' Drive left motor full reverse
i2cwrite 2,(128) ' Stor right motor
pause 1000
i2cwrite 1,(128) ' Stop left motor
i2cwrite 2,(0) ' Srive right motor full reverse
pause 1000
i2cwrite 0,(2) ' Changet to mode 2
i2cwrite 1,(255) ' Drive both motors full forward
pause 1000
i2cwrite 1,(128) ' Stop both motors
pause 1000
i2cwrite 1,(0) ' Drive both motors full reverse
pause 1000
goto main
RLY08 Relay Module
A simple example. Just switches one of the eight relays on/off.
i2cslave $70, i2cfast, i2cbyte ' Joins i2c
main: writei2c 0,($65) ' Turns relay 1 on
pause 1000
writei2c 0,($6F) ' Turns it off
pause 1000
writei2c 0,($6C) ' Turns relay 8 on
pause 1000
writei2c 0,($76) ' turns it off
pause 1000
writei2c 0,($64) ' Turns on all relays
pause 1000
writei2c 0,($6E) ' Turns all relays off
pause 1000
goto main