PICAXE Projects

PICAXE
PICAXE Microcontrollers can be programmed to do all sorts of things. Here is my account of what I have been using them for...

Nunchuck and Servo together for the first time

posted 24 Jan 2016, 00:59 by Andy Lakin-Hall

Now I have got the PICXAE to recognise and react to the Wii Nunchuck, and also drive the servo separately, not it's time to do both at once.

The next step will be to get the servo to move between two positions as the Nunchuck Z button is pressed.


The Code

Here is the code

' *** Initialisation
init:
' set servos to known position
servo C.4,100
pause 50

'set up i2c reading for pins C1 and C2 
symbol Z_button = b5
symbol C_button = b6
i2cslave $A4, i2cfast, i2cbyte
i2cwrite ($40,$00)
pause 10

lp:

writei2c (0)
pause 10

readi2c (b1,b2,b3,b4,b5,b0)

b13 = b0
b0 = b13 Xor 0x17 + 0x17 'decryption

'buttons
poke $58, bit0 'Z button
poke $59, bit1 'C button

'buttons
peek $58, Z_button 'Z button
peek $59, C_button 'C button

if Z_button =0 then 'check Z button
high 0 'LED on if button pressed
servopos C.4,180 'move servo to highest position
else
low 0 'LED off if button not pressed
servopos C.4,100 'move servo to lowest position
endif

pause 10
'debug
goto lp

Downloaded to the PICAXE and the servo moves when the button is pressed.

I've had to increase the lowest servo range up to 100 becasue I found it was sticking if it went lower. It still jumps around a bit at the lowest position, as if it is struggling to find the point. All in all the servo gives less than 90 degrees of rotation, which isn't very much. But I don't know why.

Connecting the Servo

posted 22 Jan 2016, 13:01 by Andy Lakin-Hall   [ updated 23 Jan 2016, 02:07 ]

Using the three useful holes in the proto board, I have now connected the servo.

Looking at the servo wires, the yellow wire is connected to the power supply, the brown to GND and the orange is connected to IO channel 4. 

The next task is to try and get the servo to work using the programming I did previously, but this time though IO Channel 4 rather than IO channel 0.

Coding the Servo

Recalling the following from the previous post, Interesting thing about Servos...

To get a servo to work, normally what one does is send a pulse every 20ms, and then repeat this pulse every 20ms. Once the pulse is stopped the servo will lose its position

The servo command starts a pin pulsing high for length of time pulse (x0.01 ms) every 20ms. The pulsing mode continues until another servo, high or low command is executed.

The servo command initialises the pin for servo operation and starts the timer. Once a pin has been initialised, use the servopos command to adjust position. Using a servo command again would cause the timer to reset and may cause jitter.

Here is the code from the previous article. I've taken out all the bus from the other servos and left in only C.4 commands.

' *** Program constants
' Delay to slow down servo motion (30ms default)
symbol servo_delay = 30

' *** Initialisation
init:
' set servos to known position
servo C.4,120
pause servo_delay

' *** Move forward
move_forward:
 for b1 = 120 to 180
  servopos C.4,b1
  pause servo_delay
 next b1

 for b1 = 180 to 120 step -1
  servopos C.4,b1
  pause servo_delay
 next b1

goto move_forward

Now I really have no idea if this is going to work, so I will check the Picaxe Manual http://www.picaxe.com/docs/picaxe_manual3.pdf and find out what it says about servos.

The circuit for connecting a servo is shown below;

This indicates a 6V supply for the servo, and a 330R resistor inline with the control line. There is also a 100n capacitor between the servo power supply and GND and this raises another potential issue;
Unfortunately servos require a large current (up to 1A) and also introduce a large amount of noise on to the power rail. Therefore in most cases the servo should be powered from a separate power supply, as shown below. Remember that when using two power supplies the two 0V rails must be joined to provide a common reference point.

Too much noise on the power rail would interfere with the operation of the PICAXE, and the whole thing will stop working. 

The servo I am using is a tiny micro-servo, so I need to go back and look at the specs of that micro-servo and find out what its operating voltage is before I proceed. It might turn out I need a second power supply for the servo.

Try it anyway

So I've connected the servo and downloaded the following program into the PICAXE

' *** Program constants
' Delay to slow down servo motion (30ms default)
symbol servo_delay = 30

' *** Initialisation
init:
' set servos to known position
servo C.4,100
pause servo_delay

' *** Move forward
move_forward:
for b1 = 88 to 180 step 2
servopos C.4,b1
pause servo_delay
next b1

goto move_forward


This DOES actually work; the servo slowly moves anticlockwise from position 100 to 200, then whips back to the start and goes again.

I found I could change the start and end points a bit. The manual says experiment with the range from 60 to 200. I found that if I chose a starting number smaller that 88 that the servo stuck and jittered.

YouTube Video



Wii Nunchuck tilt detector

posted 19 Jan 2016, 06:33 by Andy Lakin-Hall   [ updated 19 Jan 2016, 07:06 ]

Returning to the Wii Nunchuk tilt sensor, I have a Picaxe proto-board with an 08M chip, connected to the Wii nunchuck.

Here is the code I am using;

i2cslave $A4, i2cfast, i2cbyte

i2cwrite ($40,$00)

pause 10


lp:

writei2c (0)

pause 10

readi2c (b1,b2,b3,b4,b5,b0)


b13 = b0

b0 = b13 Xor 0x17 + 0x17 'decryption


'X joy

b13 = b1

b1 = b13 Xor 0x17 + 0x17

poke $50, b1


'Y joy

b13 = b2

b2 = b13 Xor 0x17 + 0x17

poke $51, b2


'X acc

b13 = b3

b3 = b13 Xor 0x17 + 0x17 

poke $52, b3


'Y acc

b13 = b4

b4 = b13 Xor 0x17 + 0x17

poke $54, b4


'Z acc

b13 = b5

b5 = b13 Xor 0x17 + 0x17 

poke $56, b5


'buttons

poke $58, bit0

poke $59, bit1


'X joy

peek $50, b0


'Y joy

peek $51, b1


'X acc

peek $52, b2


'Y acc

peek $54, b3


'Z acc

peek $56, b4


'buttons

peek $58, b5

peek $59, b6


if b2 < 100 then 'check X axis tilt

high 0

else

low 0

endif

pause 10

'debug

goto lp


Now what this does is tells the Picaxe to read the i2c device.

The i2c device needs clock and data signals, and these come from the Picaxe pins 1 and 2.

The output is to the LED on pin 0. It goes high if the nunchuck is tilted to the left.

The next stage of the project is to get a servo to respond as the nunchuck is moved.

What I need to do is work out how to connect a servo to the proto board.

Looking at the servo connector, there are three leads; brown, red and orange. 


Brown needs to go to ground. Red goes to the power rail. Orange is the control lead.

I need to add the three-pin connector to the Picaxe board.

I can see three convenient holes on the board over on the left hand side. 

PICAXE Proto-Board Traffic Light Simulator

posted 13 Jun 2014, 10:40 by Andy Lakin-Hall   [ updated 13 Jun 2014, 13:05 ]

One of the first things I did when I started working at Crockham Hill School was to build a set of model traffic lights that could be connected to a computer through a CoCo interface. This was pretty much the default way of interfacing lights, buzzers, motors and switches to a computer in class, and the CoCo interface is still really useful to have in school.

The CoCo kit is great - but we can only afford one. This means we have to share around the hardware between a whole class of children, and ultimately it means that not everyone can get a chance to do control technology programming.

My second problem with the CoCo system is its graphical programming style. It's great for introducing sequential programming, but it is significantly abstracted from more advanced text-based programming, which we should be teaching under the new Computing curriculum from September 2014.

I wanted to use my good experience of the PICAXE Proto-board to make a small and inexpensive programmable control box that could be used with upper KS2 classes. My prototype includes red, yellow and green LEDs, a press to make switch and a piezo speaker. This can be programmed to flash lights or make sounds, including acting on an input press from the switch. Programming can be through PICAXE Programming Editor, which is free to be used for school without charge. Obviously we would need several PICAXE USB Download Cables, but these are cheap enough to still make the project viable.

Prototype Model

On the left should be a photo of one of my prototype models. These have worked quite well. The only problems I've had have been with the cable from the switch breaking off at the board, and one or two dud LEDs.

Three AA batteries are in the pack and the battery connector doubles up as a switch  by removing one terminal and swivelling it around 180 degrees.

The piezo speaker is hot-glued to the back of the battery pack, giving it something to resonate off. The sound output is still low - enough to be heard in a classroom, but not too much to be annoying. 

Here's a drawing of what the proto-board looks like with the components in place.

Circuit Connection to LED

  • Pin 0 - Red LED
  • Pin 1 - Yellow LED
  • Pin 2 - Green LED


Circuit Connection to Switch

  • Pin 3 - Switch

Circuit Connection to Piezo Transducer

  • Pin 4 - Piezo


Parts List

 PICAXE 08M Proto Board £2.39 each
 PICAXE-08M2 microcontroller £1.80 each
 5mm LED Red £0.06 each
 5mm LED Yellow £0.06 each
 5mm LED Green £0.06 each
 330R x3 £0.72 100pk
 1k £0.72 100pk
 10k £0.72 100pk
 Mini push button switch £0.07 each
 Piezo Transducer £0.19 each
 3 x AA Battery Box (4.5V) £0.24 each
 Total £7.03

Pelican Crossing Simulation for PICAXE 08M2 Prototype Board

the_start:
low 0 'wait on green
low 1
high 2
if pinC.3 = 0 then the_start 'Decision command
low 0 'change to amber
high 1
low 2
pause 2000 'Wait command
high 0 'change to red
low 1
low 2
pause 2000 'Wait command
sound 4,(82,25)
sound 4,(67,25)
sound 4,(82,25)
sound 4,(67,25)
sound 4,(82,25)
sound 4,(67,25)
sound 4,(82,25)
sound 4,(67,25)
sound 4,(82,25)
sound 4,(67,25)
sound 4,(82,25)
sound 4,(67,25)
sound 4,(82,25)
sound 4,(67,25)
sound 4,(82,25)
sound 4,(67,25)
low 0 'change to amber
high 1
low 2
pause 250 'Wait command
low 1 'flash amber off
pause 250 'Wait command
high 1    'flash amber on
pause 250 'Wait command
low 1 'flash amber off
pause 250 'Wait command
high 1         'flash amber on
pause 250 'Wait command
low 1 'flash amber off
pause 250 'Wait command
high 1         'flash amber on
pause 250 'Wait command
low 1 'flash amber off
pause 250 'Wait command
high 1         'flash amber on
pause 250 'Wait command
low 1 'flash amber off
pause 250 'Wait command
high 1         'flash amber on
pause 250 'Wait command
low 1 'flash amber off
pause 250 'Wait command
high 1         'flash amber on
pause 250 'Wait command
low 1 'flash amber off
pause 250 'Wait command
goto the_start









New Micro Servo

posted 22 Sep 2013, 05:37 by Andy Lakin-Hall   [ updated 22 Sep 2013, 05:38 ]

Micro ServoI have taken delivery of a new micro Servo.

This is similar to the one I am currently using in my rocket connected to an RC receiver, but this one only cost me a fiver.

This one will be connected to my PICAXE prototype, and I'm hoping that this will work at 3volts. If it does, the Wii Nunchuck apogee detector is really possible.

Here's hoping!

Didn't work - but this does!

posted 16 Sep 2013, 13:29 by Andy Lakin-Hall   [ updated 16 Sep 2013, 13:29 ]

https://lh6.googleusercontent.com/-0Klc3u8HrEQ/Ui8-pQrlRWI/AAAAAAAATAI/9jqT07TdeXM/s400/IMAGE_5.jpeg
My program from the previous post didn't work. What a disaster!

Well, first I got nothing whenever the Nunchuck was connected. So I thought I'd better check the connections and lo and behold I had the adapter plugged in the wrong way round.

Once I had it in the right way round, the LED began to blink away, but would not react to any button presses. My first thought was I had damaged my Nunchuck, so I fired up the Wii to check it and thankfully it was fine.

Not being able to diagnose what was wrong I thought I'd try a cut down version of picaxester's code from http://www.picaxeforum.co.uk/archive/index.php/t-11865.html

His code used i2cslave $A4, i2cfast, i2cbyte for initialising the I2C communication, and I had read that this was deprecated in 08M, which is why I had tried the newer hi2csetup i2cmaster, $A4, i2cfast, i2cbyte. After a little editing, removing the unwanted sections, this is what I ended up with to simply read the Z Button

Code

symbol Z_button = b5
symbol C_button = b6
i2cslave $A4, i2cfast, i2cbyte
i2cwrite ($40,$00)
pause 10
Code
lp:

writei2c (0)
pause 10

readi2c (b1,b2,b3,b4,b5,b0)

b13 = b0
b0 = b13 Xor 0x17 + 0x17 'decryption

'buttons
poke $58, bit0 'Z button
poke $59, bit1 'C button

'buttons
peek $58, Z_button 'Z button
peek $59, C_button 'C button

if Z_button =0 then 'check Z button
high 0
else
low 0
endif

pause 10
'debug
goto lp
The Programming Editor seemed to like this in simulation, so I downloaded this to my 08M Proto-Board and it worked first time!

As I've only got the one LED on my proto-board I am limited to the responses the system can give me, but tweaking the code a little allowed me to come up with this program which turns on the LED if the Nunchuck is held upside down.

Code

i2cslave $A4, i2cfast, i2cbyte
i2cwrite ($40,$00)
pause 10

lp:

writei2c (0)
pause 10

readi2c (b1,b2,b3,b4,b5,b0)


'Z acc
b13 = b5
b5 = b13 Xor 0x17 + 0x17 
poke $56, b5

'Z acc
peek $56, b4

if b4 < 100 then 'check Z-axis tilt
high 0
else
low 0
endif

pause 10

'debug

goto lp

This is exactly the sort of thing I need for this project. Using the Z-accelerometer data the PICAXE can sense both the acceleration of launch and the point when the rocket turns over at apogee. 

Now what I'd really like to do is develop this into a full system that also drives a servo to release the parachute, as well as sound a beep to help locate the rocket if it lands in a gorse bush. There are a couple of channels left for me to use on the 08M, so I might be lucky. I can certainly make use of the C and Z Nunchuck buttons if I want to to arm the system. 

What I need to find out is, will a servo work at 3 volts?



Making sense of the Nunchuck data

posted 14 Sep 2013, 01:38 by Andy Lakin-Hall   [ updated 14 Sep 2013, 12:19 ]

I am trying to make sense of the data connection over I2C to the Nunchuck. Although I have some example code to work with, I'd like to understand what it supposed to do so I can debug it when it goes wrong.

My source for this is http://wiibrew.org/wiki/Wiimote/Extension_Controllers

My first query is the differences that codes are notated in. For example...

...the devices provide a virtual register block of length 0x100 that is mapped at 0xa40000 in the Wii Remote's address space (The 7-bit I2C address is 0x52).

The 0x prefix represents a number written in Hexadecimal, so "a virtual register block of length 0x100" means a number between 0 and 255 can be stored. "Mapped at 0xa40000" means the address of that storage location.

PICAXE basic shows hexadecimal numbers with a $ prefix, therefore the address of the Nunchuck in PICAXE code is $A4.

The PICAXE BASIC manual gives the following instructions about how to initialise an I2C communication with the 08M chip I want to use:

HI2CSETUP I2CMASTER, slaveaddress, mode, addresslen

Master mode is when the PICAXE controls the i2c bus. It controls other ‘slave’ devices like memory EEPROMS and can ‘talk’ to any device on the i2c bus.

SlaveAddress is the i2c slave address - in this case $A4.

Mode is the keyword i2cfast (400kHz) or i2cslow (100kHz). The Nunchuck is a 400kHz "fast" I2C device, so I will try the i2cfast command.

Addresslen is the keyword i2cbyte or i2cword. For the Nunchuck this should be i2cbyte.

So the first line of code in my PICAXE program should be...

hi2csetup i2cmaster, $A4, i2fast, i2cbyte


Once PICAXE communication has been initialised, the Nunchuck needs to be initialised to send data. You achieve this by sending the value $00 to register $40 using the command HI2COUT

HI2COUT location,(variable,...)

So to initialise the Nunchuck, the code is...

hi2cout $40,$00
pause 10

The pause of 10 milliseconds allows time for the write to take place, before reading back the data.

The Nunchuck will return six bytes of data, so the PICAXE program needs to be ready to accept this. To do this you need to use the following code...

hi2cin (b1,b2,b3,b4,b5,b0)

This loads in the six bytes of data from the Nunchuck into the PICAXE.

The sixth byte - in this case b0 contains the information about the Nunchuck buttons, so for now I can ignore the first five bytes.

The data in b0 needs to be decrypted before it makes sense. You achieve this by performing XOR $17 + $17. FIist copy bo to an unused byte b13, then perform the decryption.

b13 = b0
b0 = b13 Xor 0x17 + 0x17

Finally we need just the state of bit 0 from byte b0 to indicate the status of the Nunchuck Z button. If the button is pressed, I want the light to go out. Otherwise I want the light on.

if b0 > 0 then
    low 0
else
    high 0

So the complete program could be...

hi2csetup i2cmaster, $A4, i2cfast, i2cbyte
HI2COUT $40,($00)
pause 10
lp:
    hi2cin (b1,b2,b3,b4,b5,b0)
    b13 = b0
    b0 = b13 Xor 0x17 + 0x17
    if b0 > 0 then
        low 0
    else
        high 0
    endif
goto lp


PICAXE-08M Wii Nunchuck Interface Prototype Build

posted 10 Sep 2013, 03:27 by Andy Lakin-Hall   [ updated 14 Sep 2013, 01:15 ]

I'm building a test circuit - a PICAXE prototype with a single LED to indicate states from the Wii Nunchuck, testing out my IC2 connection.

Here is the Wii Nunchuck connector plugged in viewed from the bottom.

Wii Nunchuck connector
Next I have made up a cable and connector.
Cable to Nunchuck Connector

From left to right, the cables are as follows:

 Black gnd
 Red pwr
 Blue data (SDA)
 Yellow clock (SCL)

The 08M chip is plugged in, and the Proto board is powered up to check the LED is working.

Here is the circuit board design:

There is an LED connected to C0 with a 330R series resistor.

SCL is connected to C1 and SDA is connected to C2, both with 4K7 pull-up resistors. I will power the board from two AA batteries which should be okay to run the 08M and the Nunchuck at the same time. The Nunchuck clearly must run at 3.3 volts, so I won't risk going to 4.5v just yet. 

Here is the completed circuit ready for programming. The initial test program simply flashes the LED on and off. This works fine at 3volts, so I am going to press ahead with the Nunchuck test program.

Completed circuit

The circuit requires a program that will read the state of the Wii Nunchuck and illuminate the LED appropriately.

To start with, I just want to read the status of one of the buttons - the Z button. The LED should be ON unless the Z button is pressed. Here is the flowchart:

Initialise IC2

From the Picaxe Forum http://www.picaxeforum.co.uk/archive/index.php/t-11865.html I came across this example code to read data from the Nunchuck and display indicator LEDs - which is just what I want. There is even a video of this working on YouTube at http://www.youtube.com/watch?v=2PObPEPxPJ0

Code

i2cslave $A4, i2cfast, i2cbyte
i2cwrite ($40,$00)
pause 10

lp:

writei2c (0)
pause 10

readi2c (b1,b2,b3,b4,b5,b0)

b13 = b0
b0 = b13 Xor 0x17 + 0x17

'X joy
b13 = b1
b1 = b13 Xor 0x17 + 0x17
poke $50, b1

'Y joy
b13 = b2
b2 = b13 Xor 0x17 + 0x17
poke $51, b2

'X acc
b13 = b3
b3 = b13 Xor 0x17 + 0x17 
poke $52, b3

'Y acc
b13 = b4
b4 = b13 Xor 0x17 + 0x17
poke $54, b4

'Z acc
b13 = b5
b5 = b13 Xor 0x17 + 0x17 
poke $56, b5

'buttons
poke $58, bit0
poke $59, bit1

'X joy
peek $50, b0

'Y joy
peek $51, b1

'X acc
peek $52, b2

'Y acc
peek $54, b3

'Z acc
peek $56, b4

'buttons

peek $58, b5
peek $59, b6



if b2 < 100 then
high 0
else
low 0
endif

if b2 > 140 then
high 2
else
low 2
endif

if b3 < 100 then
high 3
else
low 3
endif

if b3 > 140 then
high 5
else
low 5
endif

pause 10
'debug
goto lp

Applying what I have found out about IC2 and PICAXE I can work out how this operates and create my own simpler bit of code just to read the Z button state.




DesignSpark PCB Software

posted 19 Jun 2013, 23:55 by Andy Lakin-Hall

Browsing through the PICAXE Software webpage I came across their suggestion to this DesignSpark PCB software.

This free software is for designing circuits and PCBs and is compatible with PICAXE systems.

PICAXE provide a library of their parts which can be imported into DesignSpark, and there is a helpful guide how to do that too.

I'm going to have a try using this to design some circuits. I have tried circuit design software before, but it has always been far too complicated for a beginner. I'm hoping that the helpful tutorials that come with this DesignSpark will get me started quickly.


Prototype Model with PEBBLE

posted 13 Jun 2013, 14:40 by Andy Lakin-Hall   [ updated 19 Jun 2013, 23:43 ]

I came across this PEBBLE software. PEBBLE is a free, open source breadboard layout tool developed by the PICAXE community for the graphical representation of breadboard layouts.

It's limited in some ways, but it does have a handy model of an 08M-Proto Board.

Here is a mock up of a circuit to test the Wii Nunchuck...

This circuit has two indicator LEDs. A red on C.0, and a green on C.4.

SDA is on C.2 and SCL is on C.1

I can't work out how to do flying leads with PEBBLE yet, so this model shows the simplest straight line connecting leads.

The header pins on the left follow from to to bottom...

+V
SDA
SCL
0V

The battery supply should be 3volts to maintain save use of the Nunchuck.

Pebble is the first really useful and easy circuit simulator I've come across. I tried Yenka, but it does not seem to allow for headers or servos and I gave up.

To get this image I got PEBBLE to print to PDF, then imported the PDF to Inkscape and converted to PNG, then cleaned up the image in GIMP before creating the JPG file. PHEW!

I guess I could have just used the Snipping Tool - but I didn't think of that!

The other thing I realised is I forgot to include the 4K7 pull-up resistors on the SDA and SCL lines :(

1-10 of 17