MOV AX, BLOG

Electronics, Photography, Gadgets

Page 2 of 3

Interfacing The PICAXE M-Series Microcontroller With A 4×3 Matrix Keyboard

After my son discovered that disclosing your PIN for a hard coded alarm is not such a great idea, we have embarked on moving our alarm system to a micro-controller which can then be programmed to allow the user to change the PIN at run-time. Our microcontroller of choice is the PICAXE which is available here. For this project, I chose the PICAXE 20M, but this was a bad idea as the memory on this device is much too small for this kind of project as I will show later. In addition to the 20M, I also ordered the AXE029 breadboard adapter, the AXE033 Serial LCD module and a 4×3 matrix keypad for data entry. I also should mention that the service from tech supplied in the U.K. is very good – I live in Germany and the package was dispatched the same day and arrived two days later.

 

4x3 Matrix Keypad

 

Our first job in this project was to interface the keypad and LCD module to the microcontroller. In this post I will describe how the keypad can be interfaced to the 20M and in a later post I will describe the LCD module and post the code for the first version of the alarm. The pins on the keypad are arranged as follows (reading left to right and looking at the keys): (1) Not Connected (2) Column 2 (3) Row 1 (4) Column 1 (5) Row 4 (6) Column 3 (7) Row 3 (8) Row 2 (9) Not Connected. The row pins (R1 – R4) should be connected to output pins of the 20M as follows: (1) Output0 -> R1 (2) Output1 -> R2 (3) Output2 -> R3 (4) Output3 -> R4. The column pins of the keypad (C1 – C3) should be connected to the input pins of the 20M as follows:  (1) C1 -> input0 (2) C2 -> input1 (3) C3 -> input2. Don’t forget that the input pins 0 – 2 on the PICAXE have to be pulled down to 0v using 10k resistors so that the pins aren’s floating when no input is present from the keypad. The circuit described above can be seen in the following schematic.

 

PICAXE 20M Keypad Interface Schematic

 

The next step is to program the microcontroller so that it scans the key pad. To do this, simply loop through the rows one at a time (output0 – output3) and test which column is set. Below is the code I developed to do this. In a nutshell, the main routine loops across all of the row pins and sets them high one by one. The keyscan routine is called for each row scan and it checks if a column pin is high – if true, it calculates the key number using the simple calculation row * 3 + column and waits until the key is released. The displayKey routine displays the number on an LCD module.

symbol row = b1
symbol key = b2
init:
pause 500
main:
key = 0
for row = 0 to 3
high row
gosub keyscan
low row
if key > 0 then gosub displayKey
next row
goto main
keyscan:
if pin0 = 1 then
key = row * 3 + 1
do loop while pin0 = 1
elseif pin1 = 1 then
key = row * 3 + 2
do loop while pin1 = 1
elseif pin2 = 1 then
key = row * 3 + 3
do loop while pin2 = 1
endif
return
displayKey:
serout 7, N2400, (254,1,#key)
return
end

Update: This post described how to interface an alternative 4×3 Keypad with a different pin-out configuration.

Amazon Kindle v2.5 Software Update

In the immortal words of Professor Hubert J. Farnsworth, good news everyone! It looks like there is an imminent firmware update for the Kindle waiting in the wings – version 2.5. The exciting new features in this release seem to be collections for organizing books and documents into one or more groups or “collections”, PDF pan and zoom, improved clarity and more fonts. It will also apparently support some social network features such as facebook, twitter and shared “highlights” or interesting passages in books – ok, but I’m more interested in its book reading capabilities. In my opinion, this fixes almost all of the niggles with the device.

Update: My Kindle DX now runs with 2.5.2 – details here.

Perfboard and Stripboard Layout Tool

As part of our Alarm2.0 project, I’ve been looking for a tool to design the stripboard layout as there are too many components to build the circuit in an ad-hoc manner. This search again highlighted one of the weaknesses of the Mac platform – it is difficult to find niche freeware. I had a similar problem with pdf tools for the KindleDX and eventually purchased PDFPen because I couldn’t find a decent alternative. Happily, this time I’ve found something which fulfills my simple stripboard layout requirements – DIY Layout Creator version 2.

DIY Layout Creator 2

Free Textbooks For The Kindle DX

There is a surprising amount of high quality free textbooks available on the internet in PDF format if you spend some time searching for them. Two sites which are definitely worth taking a look at are: E-Books Dictionary which has over 3300 free e-books in over 400 categories and Free Computer Books. Most of the e-books on these sites seem to be available in PDF format which is perfect.

Dive Into Python

Alarm Project 2.0 Beta

Our second version of the alarm project is now in beta! All we have to do now is update our design in circuit simulator, do a tiny bit of debugging and move it all over to perforated board. Over the next few weeks, there will be a series of posts describing how each module of the alarm circuit works. This design consists of 5 J-K Flip-Flops, 8 AND gates, 2 OR gates, 4 NOT gates, 1 NPN transistor, 3 555 timers, a darlington driver, a 5v relay, a 5v voltage regulator, 12 diodes, 7 LEDs, a magnet switch, a speaker, a piezo buzzer, a numeric keypad, various resistors and capacitors and lots of single core wire and time.

Alarm 2.0 Beta

PICAXE Traffic Light Project and Using PICAXE 20M Interrupts

In a previous post describing how to build a traffic light (or stop light) system using a PICAXE microcontroller, I did not include the details of how to implement a pedestrian crossing part of the project. This post will describe the two ways we tried to implement the feature. Our first attempt used the interrupt feature of the PIXACE 20M and a push button connected to +5v and input pin 5 on the microcontroller. By using the setint instruction, the PICAXE can detect changes on its input pins and match given states, for example: setint %00000000, %00100000 will trigger and interrupt when pin 7 is low and setint %00100000, %00100000 will trigger and interrupt when pin 7 is high. Once an interrupt has been activated, the PICAXE does a gosub to the subroutine at label interrupt and returns to the place it left from on completion. Below is the code we implemented for the first version of the pedestrian crossing using the interrupts described:

symbol rot = 1
symbol gelb = 2
symbol gruen = 3
symbol fgrot = 5
symbol fggruen = 4
let w0 = 0
setint %00100000,%00100000
high fgrot
ampel:
high rot
if w0 = 1 then
gosub fussgaenger
let w0 = 0
setint %00100000,%00100000
else
pause 3000
endif
high gelb
pause 1000
low rot
low gelb
high gruen
pause 3000
low gruen
high gelb
pause 1000
low gelb
goto ampel
interrupt:
let w0 = 1
return
fussgaenger:
high fggruen
low fgrot
pause 3000
high fgrot
low fggruen
return

Unfortunately, this design has a pretty big issue. Namely, when the push button is pressed, the interrupt breaks out of pause statements and does not return to within the pause but immediately after it. This means that a pedestrian can influence the speed at which the traffic light system switches from one phase to another. To get around this, we decided to replace the use of interrupts by a hardware flag in the form of a latch built from an AND gate and a diode. When the user presses the pedestrian crossing button the AND gate latches the input and during the red phase, a check is made to see if the button was pressed. If it was, the pedestrian light sequence is started and the program loops around as normal. In the picture below, you will be able to see the AND gate latch, the traffic light LEDs and the pedestrian crossing LEDs.

PICAXE 20M Traffic Light Project With Pedestrian Crossing

The schematic for this circuit, drawn using tinyCAD, can be seen below.

PICAXE-20M Traffic Light Schematic

Below is the final piece of code for the hardware shown above. There are of course, improvements that could be made, but for the time being, I think that this was a good first step into the PICAXE world.

symbol rot = 1
symbol gelb = 2
symbol gruen = 3
symbol fgrot = 5
symbol fggruen = 4
high fgrot
ampel:
high rot
if  pin5 = 1  then
pause 800
gosub fussgaenger
else
pause 3000
endif
high gelb
pause 1000
low rot
low gelb
high gruen
pause 3000
low gruen
high gelb
pause 1000
low gelb
goto ampel
fussgaenger:
high fggruen
low fgrot
pause 3000
high fgrot
low fggruen
pause 800
return

Ohm’s Law

This video from Make Magazine is a great introduction to ohm’s law and a good way of bringing some of the science behind electrical circuits to kids and adults alike.

[youtube=http://www.youtube.com/watch?v=-mHLvtGjum4]

A First Simple PICAXE Microcontroller Project

Some of my son’s other interests include traffic lights (or stop lights), boom barriers, automatic doors and control panels of any kind. The first microcontroller project we decided to build was a traffic light system including a pedestrian crossing. The microcontroller kit I decided to use for this was the PICAXE-20M Starter Pack (USB) which uses a PICAXE 20M with 8 input pins, 8 output pins, 220 lines of program memory and supports interrupts, digital temperature sensors, radio-control servos, keyboard input, user defined musical tunes, infra-red transmission and reception, an 8/10 bit ADC option, pwm motor control and input pulse counting. The device is programmed via the supplied USB cable. It is a surprisingly sophisticated chip for 2,35 euros. Below is an image of the 20M connected to a small breadboard with three LEDs (one red, one yellow, one green) and 330 ohm resistors.

PICAXE 20M Traffic Light Project

In the circuit above, we connected output pin1 to a 330 ohm resistor and then a red LED, output pin2 to a 330 ohm resistor and then a yellow LED and finally output pin3 to a 330 ohm resistor and then a green LED. To program the PICAXE, you will need a copy of the MacAXEPad for the Apple Mac or AXEPad for the PC which is available at the PICAXE software download page. There is also a comprehensive set of manuals online which describe how to develop code for the PICAXE in BASIC. The basic program below controls the traffic light and is my son’s first ever piece of code. :-)

tlight:
high 1
pause 3000
high 2
pause 1000
low 1
low 2
high 3
pause 3000
low 3
high 2
pause 1000
low 2
goto tlight

Alarm 2.0 and Microcontrollers

The next projects we have been working on and will be described in later posts are much more sophisticated than the previous alarm system. The projects include: building logic gates such as AND, NAND and OR out of NPN transistors, a four bit adder out of logic gates which in turn are built from NPN transistors, a 4 bit counter from J-K Flip-Flops, a new version of our alarm system using 555 timers, logic gates, diodes and a keypad to activate and de-active the alarm and finally, a traffic light system using a PICAXE microcontroller and logic gates. If you would like to find out more about these components and electronics in general, I can highly recommend Make: Electronics by Charles Platt.

Make: Electronics

If you would prefer an online source of information, I can also recommend doctronics which has some great guides and projects with useful circuit diagrams in breadboard format. Take a look at the doctronics biscuit tin alarm for a fun project.

From Breadboard to Perforated Board

The next phase in the development of our first alarm system was to move the components from the breadboard we had been using for prototyping to a perforated board on which we could solder the components and install it in my son’s room. To do this, you will first need to learn how to solder and there are some great online soldering tutorials such as the one below.

[youtube=http://www.youtube.com/watch?v=I_NU2ruzyc4]

I personally chose a 15 watt soldering iron as I will only be using it to solder electronic components and the lower power should help protect the components from being damaged through exposure to too much heat. I recommend that you also purchase a vacuum de-soldering tool and a decent stand. What I have also found important is a third hand such as the one pictured below. This is a great tool for holding your board while soldering components onto it.

The Indispensable Third Hand

After a few minutes of practice, we were able to construct the simple alarm circuit described in a previous post onto a perforated board with little difficulty. The final result can be seen below.

Simple Alarm Circuit on Perforated Board

 

« Older posts Newer posts »

© 2017 MOV AX, BLOG

Theme by Anders NorenUp ↑