High Performance, Low Cost…

USB-PICAXE-20X2 STEM project board

Stacks Image 280
Microcontrollers are widely used in instrumentation because they are inexpensive and versatile. The USB-PICAXE-20X2 allows easy experimentation using the PICAXE 20X2 chip and is ideal for STEM projects/prototyping experiments. PICAXE chips are very easy to program in BASIC using the PICAXE Editor (at no cost).

The small PCB (shown at left) houses the microcontroller chip and gives access to its 16 I/O pins, as well as having a USB interface that allows user programs to be loaded into the microcontroller.

The PCB derives power from a host PC when connected via the USB cable. Once programmed, the PCB can be disconnected from the PC and run stand-alone with power coming from a small plugpack adapter.

Headers can be soldered into the USB-PICAXE-20X2 PCB so that it can be conveniently mounted onto a prototyping board, giving access to all pins on the X2 chip.

The diagram above shows the pinout of the PICAXE-20X2 chip. In addition to power (+V), GND (0V) and the Serial In and Out pins there are 16 general purpose I/O pins, most of which can also be used for analog-to-digital conversion (ADCn). The chip also supports both the i2c and SPI protocols as well as hardware interrupts. In addition, the 20X2 offers an advanced PWM mode on 4 of its pins, and 2 analog comparators.

Here is a summary of the main features of the 20X2 from a programming standpoint, sourced from the PICAXE documentation. :

Clock Frequency

The default power-up operating frequency is 8MHz, using the internal resonator. The 20X2 has internal clock frequency options up to 64MHz.


General RAM
The PICAXE-20X2 has 128 bytes of general RAM. 56 of these bytes, known as b0 to b55, can be used in any command. All bytes (0-127) can also be addressed both directly and indirectly.

To directly address bytes in the general RAM space the peek (read the byte) and poke (write the byte) commands are used. Note that peek and poke are dedicated to the general purpose variables, to read the microcontroller peripheral registers the commands peeksfr and pokesfr are used.

To indirectly address the values the virtual variable name ‘@bptr’ is used. @bptr is a variable name that can be used in any command (ie as where a ‘b1’ variable would be used). However the value of the variable is not fixed (as with b1), but will contain the current value of the byte currently ‘pointed to’ by the byte pointer (bptr).

The compiler also accepts ‘@bptrinc’ (post increment) and ‘@bptrdec’ (post decrement). Every time the ‘@bptrinc’ variable name is used in a command the value of the byte pointer is automatically incremented by one (ie bptr = bptr+1 occurs automatically after the read/write of the value @bptr). This makes it ideal for storage of a single dimensional array of data.

Scratchpad RAM
The PICAXE-20X2 also has 128 scratchpad bytes. To directly address the scratchpad values the get (read the byte) and put (write the byte) commands are used.

To indirectly address the values the virtual variable name ‘@ptr’ is used. @ptr is a variable name that can be used in any command (ie as where a ‘b1’ variable would be used). However the value of the variable is not fixed (as with b1), but will contain the current value of the byte currently ‘pointed to’ by the pointer (ptr, which is now a word variable, made up of two bytes ptrl and ptrh).

The compiler also accepts ‘@ptrinc’ (post increment) and ‘@ptrdec’ (post decrement.

Input and Output Pins

In the X2 chips almost every pin is configurable as an input or an output.
Pins are referred to by the notation format PORT.BIT e.g.

high B.0
count C.2,1000,w1

When using input pin variables (e.g. within if..then commands) the notation pinPORT.BIT is used as the variable name.

if pinC.3 = 1 then …

The whole port can be read or written by using the variable name pinsX

let b1 = pinsB ‘ reads the state of the input pins
let b1 = outpinsB ‘ reads the state of the output pins
let outpinsB = %10101010 ‘ controls the state of the output pins

All pins (with the exception of the download serial output pin) are configured as digital inputs at power-up. Most output commands (high, low, pulsout, serout etc.) automatically convert the pin to an output. However the configuration of the pins can also be controlled by the dirsX variables or the input/output/reverse commands.

let dirsB = %11110000

Hardware Interrupt Pins

The 20X2 has 2 pins that can be configured as hardware interrupt pins. When correctly configured, these pins continuously background scan for an edge based trigger, even during sleep. When this trigger occurs a flag is set which can be used to trigger a ‘setintflags’ event. See the ‘hintsetup’ command for more details.

Analog Inputs
Analog pins are configured using the adcsetup variable

let adcsetup = %00000011

Using the ‘readadc’ command does not automatically configure the pin as an analog input. Pins must also be set as inputs (not outputs) for an analog input to work correctly. The analog voltage range can be the PICAXE power supply range or an alternate external voltage range. In this case two analog pins are used to set the positive and negative reference for the ADC.

Each ADC is given a unique ADC channel number. This is the number used in the readadc command (e.g. to read analog input 4 into variable b1 one would use readadc 4,b1.

The PICAXE 20X2 has two internal comparators (C1 and C2) which constantly compare two analog values. The two values can be two external ADC pins, or one external ADC pin and an internally generated, configurable, voltage reference.

The comparator outputs are always available in the ‘compvalue’ variable. If desired the comparators can be setup to trigger a flag, which can then be used in a ‘setintflags’ interrupt routine. See the compsetup in the 20X2 documentation for more details.

Project Work

Here are some very simple introductory projects that I’ve used to introduce high school students to basic programming using the PICAXE project board. Once digital and analog I/O have been successfully mastered, explorations can become more complex, open-ended and group-based activities.

With a large class of students divided into project groups a useful approach is to reserve several sessions for group activity (programming and circuit construction using prototyping boards), followed by a final session where each group presents its project work to the class.

Pedestrian Crossing Traffic Lights

Using a switch, a red LED (Light Emitting Diode), a green LED and the PICAXE project board, build a set of traffic lights that normally shine green, but when a button is pressed and released, change to red for 20 seconds, then automatically return to green.

Night Light
Using an LDR (Light Dependant Resistor) to measure light intensity, two LED’s for lights and a PICAXE project board, build a night light where one LED automatically comes on as it gets dark and switches off when it becomes light, while the other LED functions in reverse (Ie ON in the light; OFF in the dark).

Lamp Dimmer
Using a 10k potentiometer, a white LED and a PICAXE project board make a lamp dimmer, using an ADC to read the voltage at the potentiometer’s wiper and use this to control the LED brightness using a PWM waveform.

Alcohol Breath Tester
The FIGARO TGS 822 is a gas sensor for detecting organic solvent vapours in applications such as:
• Breath alcohol detectors
• Gas leak detectors/alarms
• Solvent detectors for factories, dry cleaners, etc
The sensing element in the device is a tin dioxide (Sn02) semiconductor that has low conductivity in clean air. In the presence of a detectable gas, the sensor's conductivity increases depending on the gas concentration in the air.

Using a FIGARO TGS 822 gas sensor and the PICAXE project board, build a detector for alcohol in breath. Output the detector’s measurements to a computer and display it in real time as a graphical output on the screen using a LabVIEWTM program. If you’d like more details about using the PICAXE project board with LabVIEWTM front panels, contact me at