PICAXE Microprocessor Tutorial

Information Sources

Picaxe Microcontroller Projects by Ron Hackett
Picaxe Manuals 1, 2 & 3 from www.picaxe.co.uk
Picaxe Forum

Power source

The PICAXE microprocessors has a maximum voltage of 5.5v. My choice was for 3 x AA alkaline batteries giving 4.5v housed in a switched box. A male Hitic R/C connector was soldered on so the power source can be connected directly into your RX BATT. A separate female connector is used to connect the power source directly to the "Problock".
Manual 1 pg 25.

PC download jack

A USB connector cable has to be purchased as it has electronics in the USB connector. It connects your PC to the processor. A 3.5mm stereo jack socket was fitted to a 4 x 12 hole stripboard. The 5 holes have to be drilled out 1.6mm. The longer legs of the Header Block have to be inserted through the stripboard.
Hackett pg 11
 
picaxe jack pcb picaxe jack
 

1. My first "Hallo"
Manual 1 pg 27

hallo circuit diagrams Just to confuse you from the very start, the chip pin numbers are not used in the computer code. To confuse you even more, Hackett’s book refers to a 08M2 but this is not available!
The 08M chip has three dedicated pins for power supply +ve 1, -ve 8 and data in 2 (serial/serin2)
All the other five pins are used for I/O Ports (Inputs and Outputs). On the 08m they are numbered in the reverse order to the pin numbers.
 
Number 1 can refer to the actual chip pin, input/output port or binary 1 = on/high. In Manual 2 Basic Commands coding you will see C.1, low B.1, pinC.1 The best advice for the 08M is to use just the number “high 1” but add the comment “port 1 = pin 6”
 
From PICAXE Forum Technical Moderator With the move towards X2 and M2 parts the port.pin (C.1) notation will become more common and compilers have been updated to support such notation for earlier PICAXE to aid porting of code between variants. See Manual 2 page 27
 

 

Output by LED

hallo circuit diagrams The data transfer jack socket circuit uses pin 2 for data in and pin 7 (port 0) for data out.
The 22K resistor drops the voltage/current into the chip. The 10K grounds the data signal when it goes low otherwise the chip will not work correctly.
Grounding make the terminal’s voltage near to the power supply negative.
Manual 1 pg 43.
 
hallo circuit diagrams problock with jack socket A LED is added as the output to pin 6 (i/o 1). A 330R resistor is added to give 2v across the LED.
The LED is connected across pin 6 (i/o 1) and ground (0v). Its current comes from pin 6 (i/o 1) when it turns on = goes High. The LED is “Sourcing” the current from the chip.
Manual 1 pg 72.
 
hallo circuit diagrams If the LED circuit is connected to the +ve rail, Pin 6 (i/o 1) is near 0v when it goes Low. The LED would turn on as it is getting its current from +ve rail and its ground (negative) from the chip. The LED is “Sinking” its current from the chip.
Manual 1 pg 72.
 
hallo circuit diagrams If you remove the data input jack socket you must add a 10K resistor from pin 2 to ground. Small blue resistor top centre. Manual 1 pg 29.
 
problock without jack socket
 
hallo circuit diagrams With both LEDs connected, you can see they both can be controlled from one Output alternating their High’s. The purpose is to show that a negative source for components do not have to come from the Battery’s negative.
 
problock without jack socket


 

 

My first "Hallo" computer code

This code from Manual 1 pg 72.
 
Main:
      high 2          ; i/o pin 2 = phyiscal ic pin 6
      pause 750
      low 2
      pause 1500
      goto main
 
Main:     Label or Address of a specific piece of computer coding. Can be reused.
:            This means it is a new label.
high 2    Makes pin 2 an Output and high. Can source or sink an external circuit.
low 2     Makes pin 2 an Output and low. Can source or sink an external circuit.
pause    Pauses the program for a specified time. 1000=1sec
goto      Permanent jump to a specified address/label. Loop back to start.
 
             High is a voltage above 80% of power supply. Low is 20% below.
 
problock with jack socket
 

 

2. Input by Mechanical Switch
Manual 3 pg 27

Any component that has mechanical contacts that make or break the circuit cause problems. The contacts ‘Bounce’ when they make (close) and ‘Arc’ when the break (open).
As the chip run quickly, it can count the on/off bounces so sending a chain of High/Lows when all you want is one single High. Code can add a routine to pause for a few milliseconds until the Bounce has settled.
Arcing occurs at the moment the contacts open as the current bridges the gap causing a spark that can still transmit some current. The chip senses this as still being High (on). Normally this is not a problem but the arcing can cause electrical ‘Noise’ interfering with the programme as it is being read.
A 10K resistor is added to keep the Input Low when the switch is open.
 
Input switch  Problock circuit
 
Input switch circuit init:
      let b0 = 0
 
main:
      if pin3 = 1 then add       ; i/o pin 3 = physical ic pin 4
      if pin3 = 0 then low 1
      endif
      goto main
 
add:
      pause 100
      let b0 = b0 + 1
      if b0 < 5 then main
      high 1                           ; i/o pin 1 = physical pin 6
      goto main
 

 
b0
A variable named b0 used to store numbers between 0 and 255 (no negatives or fractions). Named b0 , b1, ...
Manual 1 pg 52
 
init:
Means initiate. A section of code at the very start of a programme which is only read once. Variables can be given initial values b0 = 200
 
Comments
Explanations of code for future reference. Started with ; or ' ignored by computer.
 
White spaces
Used for clarity only one is recognised by the computer. Manual 1 pg 47
 
if ... then
Used to find the state High/Low of an input and then do some operation. if pin4 = 1 then ..
 
if pin3 = 0 then low 1 and endif were left off the code in the manual. They are used to turn the output Low when the switch is turned off.
 
endif is needed to close the If statement

 
b0 < 5
If the value of the variable b1 is less than 5 then do some operation.
 
let b0 = b0 +1
Adds 1 to the variable b0. Used to count the number of times to programme loops through the code before doing something else. Input switch flowchart
 

 

3. Input by variable resistor
Manual 3 pg 28

Variable resistor as input  Problock circuit
 
Input variable resistor circuit init:
b1 = 0
 
main:
readadc 4, b1                'read value on pin 4 into variable b1
if b1<75 then light1       'if b1 is less than 75 then light 1 Green Led
if b1<175 then light2     'if b1 is less than 175 then light 2 Red Led
goto light3                   'if b1 is greater than 175 then light 3 Both Led’s
 
light1:
high 1          'pin 1 switch on LED 1 Green
low 2            'pin 2 switch off LED 2 Red
goto main     'loop
 
light2:
low 1           'pin 1 switch off LED 1 Green
high 2         'pin 2 switch on LED 2 Red
goto main    'loop
 
light3:
high 1          'pin 1 switch on LED 1 Green
high 2          'pin 2 switch on LED 2 Red
goto main    'loop
 
readadc
Read analogue data on input pin 4 into variable b1. Only use readadc ports 1, 2 & 4. Maximum impedance is 20K.
 
At change points 75 & 175, VR values measured/calculated = 820R/730R & 1760R/1700R


 

 

4. Input by Heat and Light Sensors

Thermistor/LDR circuit Thermistor/LDR circuit init:
b1 = 0
 
main:
readadc 4, b1                                'read value on pin 4 into variable b1
if b1<105 then light1                      'if b1 is less than 150 then light 1 orange
if b1>106 and b1<120 then light2   'if b1 is more than 151 and less than 200 then light 2 green
goto light3                                    'if b1 is greater than 200 then light 3 red
 
light1:
high 0        'pin 0 switch on LED orange
low 1          'pin 1 switch off LED green
low 2          'pin 2 switch off LED red
goto main   'loop
 
light2:
low 0          'pin 0 switch off LED orange
high 1         'pin 1 switch on LED green
low 2          'pin 2 switch off LED red
goto main   'loop
 
light3:
low 0          'pin 0 switch off LED orange
low 1          'pin 1 switch off LED green
high 2         'pin 2 switch on LED red
goto main   'loop
 
b1>105 and b1<120 b1 more than 105 and less than 120.
low 1 Adding ‘low’ code lines means only ‘high’ LED will light.
 
These sensors work by varying their resistance depending on tempreture and light levels. Resistance falls as heat/light levels increase.
The sensors form the +ve side of a Potential Divider with a 4K7 resistor to ground (-ve). 4K7 was chosen as it matches the 5K Thermistor. The output is analogue not digital.
 
The programme code is similar to the "Variable Resistor" but the values between 0 - 255 will have to be found either by measuring resistance at the change over point or by trial and error.
 
One point of interest is how quickly these sensors respond to change. This can be found from “Rise and Fall” times if given.
 
Heat Sensor
5K NTC Disc Rapid 61-0410 £1.22
 
Light Sensor
NORPS12 LDR - Dark resistance 1M - @1ftc 5K4 min 12k6 max - Rapid 58-0132 £0.88p
LDR - r/fall 30ms - Rapid 58-0135 £0.72p (quicker r/fall time than above)
 
LDR details page 17
 
Having made up these circuits I came across a Forum Post explaining how to calculate the fixed resistor’s value. 8K2 for light and 1K for heat. So the values I used in the code will have to be altered to suit the sensor’s specification.
 
Picaxe Forum Choosing resistor
Potential Divider


 

 

5. Input by IR Optoswitch

Ir counter circuit problock Ir counter circuit do
    if pin4 = 1 then high 2       ; pin 4 = physical 3   high = green
    elseif pin4 = 0 then low 2   ; pin 2 = physical 5   low = red
    endif
loop
 
do ... loop   is the same as “main: ... goto main” in simple codes.
 
Optoswitches are self contained components housing matching Infra Red (IR) emmitters and sensors. The switching off (low) is achieved by placing an object in the slot cutting off the IR light. Its function replaces micro switches limiting movement or for counting shaft revolutions.
 
The three components listed are basically the same size but have diffent response rates. Check Rapid’s datasheets for more information.
 
H21A6 Rapid 58-0303 Response 3µs. Rise/fall 8/50µs £1.29
KTIR0611S Rapid 58-0942 Rise/fall 5/25µs £0.38
KTIR0221DS Rapid 58-0310 Rise/fall 90µs £0.50 Includes a built-in transistor
     forming a Darlington Pair increasing sensitivity but not response time.


 

 

6. Switch pulse counter

Input switch  Problock circuit
 
Input switch circuit Code from Piers and others on Picaxe Forum
 
symbol bounce = b0       ; gives variable b0 the name bounce
symbol counter = b1      ; gives variable b1 the name counter
bounce = 0
counter = 0
 
bound:
if pin3 = 1 then add         ; goto the switch bounce routine add
if pin3 = 0 then bound     ; waits for input high
 
add:
pause 100
let bounce = bounce + 1
if bounce < 5 then bound
 
counting:
do
loop while pin3 = 1       ; Sit here while pin3 is high
inc counter                  ; increment by +1
if counter>5 then         ; check for condition counter higher than 5
high 1
endif
do
loop while pin3 = 0      ; Sit here while pin3 is low
goto counting             ; loop back waiting for the next high/low
 
Symbol
Using variables such as b0 can become confusing in complex codes, you forget which one is which. Symbol is used to give a recognisable name to each variable ‘bounce’, ‘counter’. You could not use ‘count’ as this is a restricted word and would appear blue in the programme editor and through up an error message in the simulator.
Manual 2 page 232
 
bound
These two lines make the programme wait for input pin3 to go high
 
add
Switch contact bounce routine explained earlier in ‘2. Mecanical Switch’
 
Counting
do loop while pin3 = 1 makes the input wait in high state until it goes low. This could be up to 30min+.
When the input goes low the program goes to the next line.
 
inc counter This is short for counter = counter + 1 which increases the variable b1 (counter) by one each time it loops through.
 
if counter>5 then When the variable ‘counter’ reaches the value of 5 it exits out of the counting loop to the next line of code.
 
do loop while pin3 = 0 This line is to keep the output high even when the input goes low, keeping the output continually high.


 

 

7. Input shaft direction

Dual optoswitch sensor for shaft direction Dual optoswitch sensor pulse If the disc stops before pulse b and then goes forwards, it will read b then a + c = Forwards. However if the disc stops between pulse b and a and goes forwards it will be read as a + c then b which is logic reverse. If you use different lengths of low pulses, it will still give mis-readings of direction.
 
The only solution is to build a ‘Quadrature Encoder’ which looks extreamly complex, altenatively buy one for £40.
Quadrature Encoder Tutorial
 
The obvious solution is to take a feed from the Motor Controller inputing a High for Forwards and Low for reverse. This can be simulated by a simple switch input into the ‘Counting’ coding.


 

 

8. Shaft bi-direction counting

Counting in both directions Counting in both directions
 
This module is part of the control system for my servoless self tailing winch. It counts the number of rotations of the shaft to let the sail sheet out (forwards) and pull it in (reverse). Total sheet travel is seven turns = 7 x 12 slots on the disc = 84 pulses.
 
To set the counter to zero, the sail sheet is left fully in when turning the power off. The counter is set to zero when the power is turned back on. b1 = 0 (reset facilities may be required).
 
The position of the Joystick controlling the Speed Controller can be used to set the direction of shaft rotation. Above 1.5ms = forwards and below = reverse. This avoids to complication of having multiple sensors see Quadrature Encoders in previous module.
The direction can be sent to the counting module as a High = forwards or low = reverse on pin4. A switch can be used to simulate this input to test its validity.
The output pins 1 and 2 signals are sent back to the speed controller to stop the motor and to restrict the motor to turn only in certain directions.
 
Results
 
When the shaft turns forwards 84 slots, it turns the output pin1 on sending a High to the speed controller turning off the motor and restricting any forwards motion. However, when the shaft is reversed, the forwards output signal is not turned off stopping the motor from ever turning forwards. The green led remains on. I tried putting “if counter > 80 then low 1” in “countingback” but the whole code stopped working.
 
[CODE]
 
symbol MOTOR = pin4
symbol SLOT = pin3
symbol counter = b1
counter = 0
 
main:
if MOTOR = 1 then forinput       ; high input from speed controller motor forwards
if MOTOR = 0 then backinput     ; low input from speed controller motor reverse
 
forinput:
if SLOT = 1 then countingfor
if SLOT = 0 then forinput         ; waits for input high
 
countingfor:
do
loop while SLOT = 1           ; Sit here while pin3 is high
inc counter                        ; increment by +1
if counter>84 then high 1   ; check for condition b1 higher than 84 then green
endif
goto main                         ; loop back waiting for the next high/low
 
backinput:
if SLOT = 1 then countingback
if SLOT = 0 then backinput         ; waits for input high
 
countingback:
do
loop while SLOT = 1            ; Sit here while pin3 is high
dec counter                        ; increment by -1
if counter<6 then high 2      ; check for condition b1 less than 6 then red
endif
goto main                          ; loop back waiting for the next high/low
 
[/CODE]
 

 

9. Input by R/C receiver

Marine R/C users will want to control their Picaxe microprocessor with their Transmitter/Receiver. Let’s look at the signal coming from the Receiver.
Manufacturers don’t seem to want to disclose this information; there are no standards. All I have found out is what signal is required by analogue Servos. Who knows what is the Digital signal?
 
Servo PWM specification
Hitec 0.9 to 2.1ms — 1.5ms mid-point
Futaba 1.1 to 1.9ms — 1.52ms mid-point
3 — 5v peak to peak square wave pulse
Peak refresh rate 50Hz (20ms)
Dead band 20 — 40 micros Input by rx Rx input to Picaxe
The white/orange signal wire is connected to pin3.
The red +ve power feed from the Rx can be connected if the Picaxe is used to supply power to the Rx. It may be advisable to separate the power sources as this avoids any electrical noise reaching Picaxe from the servo motor .
The black/brown earth connection must be used as it is the signal’s ground.
 
Picaxe reads Rx signal
Manual 2 page 155
 
From Westaust55 Picaxe Forum
The values from the PULSIN command are in increments of 10 usec.
Thus a pulse of 1.1 msec = 1100 usec which will give a value of 110 in the variable associated with the PULSIN command.
Likewise the SERVO related commands generate pulse durations in increments of 10 usec
Thus with the command SERVOPOS 180
This equates to a duration of 75 * 10 = 750 usec or 750 / 1000 = 0.75 msec.

 
Pulsin 3,1,w1
Reads the signal on ‘pin3’ and puts its value in the variable ‘w1’
‘w1’ is a word variable as it accepts values upto 1024. (Byte values upto 255) If you are using “Byte b0 and Word w3” variables in the same code be careful of the numbers. b1 and w1 are not alllowed. Read Manual 2 page 10
‘1’ starts the timing when the pulse goes from low to high.
 
[CODE]
 
Init:
w1 = 0
 
main:
pulsin 3,1,w1                        ; code to finding central point
if w1= 144 then high 1          ; light on and remains on at 144
elseif w1 = 146 then low 1     ; light off at 146
endif
goto main
[/CODE]
 
Test
Connect up circuit and see where the joystick is positioned to make the leds light up. Try going max, min and central joystick positions to establish your Rx characteristics. Use these figures for your coding.
 
Results
Most Transmitters have a switch to alter the direction of servo travel and a lever to make small adjustments to the central point. Minimum — Mid point — maximum stick position.
Normal 40Hz      min 100 — 146 — 185 max values of w1. Joystick adjuster +/- 8
Reverse 40Hz     min 190 — 146 — 100 max.
Normal 2.4GHz   min 118 — 146 — 180 max.
 
Testing Servos
When testing servos over a year ago I built two ‘Servo Tester’ black boxes to save time getting out the transmitter & receiver. It was also possible to add a scale in msec. You can find details and illuminating results. Look at the Arm and Drum servo tests.
Tester results 0 to 2.4ms  min 280 — 120 — 15 max values of w1.
 
What is a Servo
 
The Hitec sail winch HS785-HB will make 9 turns from 0.5 to 2.05ms. The box says 3 to 4. Above and below these values, it will run continuously. “Not a lot of people know that”


 

10. Other inputs

As I am interested in r/c marine applications, inputing using Infra red for remote control and distance sensing has been omitted. Many other types of sensors can be inputed using the previous methods.


 

11. Output by LED's

The two fundamentals of LED’s are they only work in one direction and their voltage is restricted.
The symbol looks like an arrow with a bar at the pointed end. The arrow shows the direction the current must flow through the LED.
If you look at an LED one wire is longer than the other. The long one goes to the +ve supply. If the wires have been cut, you will see a flat on the flange next to the -ve terminal.
To remember which is positive PLANS = Positive is Long And Negative is Short. This is used to get the symbol for a battery the corrrect way round.
The bar is used to indicate the band on normal dioeds.
 
It is normal practice to add a resistor in series to the LED to restrict the voltage across the LED. Search their specification and look for their Forward Typical Voltage. 2v is a good guide.
Typical LED spec is 10mA at 2v. With a 4.5v supply, the voltage across the ballast resistor is 2.5v. Resistor value = 2.5 / 0.01 = 250R. Using 330R, the voltage across the LED = 4.5 - 330 x 0.01 = 1.2v


 

12. Output by Motor driver L298

Output by L298 motor driver
 
Circuit Sources
Ron Hackett’s Picaxe Projects page 228
ST Data sheet
IKAlogic
PyroElectronic Tutorial
Picaxe forum
Alan Bond at Technobots 220R added to “prevent large motor earth return current flowing into radio control system”. Alan also suggests adding a transistor into the signal input to clean it up especially for 2.4GHz transmitters.
 
Power Source
Three Picaxe modules will be needed to control four motors with two paired for twin gib sheets. They will be supplied from their own source to reduce motor electricl noise. The Receiver will be powered separetely with the rudder servo. The motors will not be run continuously but need 8 Amps in total.
 
Joystick Control
For my winch control, it is not neccessary to have proportional control. All that’s needed is Fast and Slow forwards, Off, Slow and Fast reverse. Let the sail sheet out and pull it in.


 

13. Output by servo

Basic Servo Tutorial by Wetaust55