In Part 1 we blinked an LED using the Commodore 64 User Port, but outputting is only half of the equation so in this video we use the INPUT to alert us when various sensors are tripped.
Reading the User Port with C64 BASIC
If you recall, the C64 User Port has a Direction Register at 56579 (see previous article for PET and Vic 20) that we use to tell the computer which of the 8 pins we are using should be input or output. In this case we want all input so we set them all with
After this, it is a simple case of
PEEKing 56577 to read the state of those pins, are they receiving any current or are they being pulled low to ground?
Connect the 8 pins to a breadboard, and a jumper wire to Ground.
This quick BASIC program will show the cumulative value of the 8 pins as the inputs change as you connect a pin to ground, taking the input to low:
So what is happening here?
By default the pins are pulled HIGH when set to input. You might recall that as soon as everything was powered up with an LED connected your LED lit up somewhat.
This means the pin will read as if current is flowing, but when we connect it to ground it becomes a definitive zero. If the pin we connected Ground to was the first pin then the
PEEK will read 254, because 255-1=254. If the last pin is connected instead we take away 128, giving us 127 as the result.
Try it out, put a wire from pin 0 on the long leg of an LED, with a resistor (220ohm or above) and ground on the other side.
What do you get when you do
Keep in mind this mode of operation as you try different sensors, some are “active” by passing current, others by restricting it.
Let’s show this by using a proximity sensor
C64 Motion Alarm
PIR, or “Passive Infrared” sensors, detect if a warm body has moved in or out of range.
While for our purposes we are not going to be buying anything industrial-grade, they are a staple tool of the security alarm trade as in most cases it is warm bodies the alarm people are watching out for.
Some work as relays but most that we will come into contact with as hobbyists work like a simple switch.
Usually, on one side we have a + pin, meaning we need to connect 5v. On the other side is a -, which we will connect to Ground.
The magic happens in the middle pin, which is where we get our signal. Check carefully to see how yours is arranged before connecting anything!
Once you are sure everything is connected, try out the code above again and wave your hand over the sensor. You might get a delayed response but you should see your LED light up and the user port pin readout change …
But what is the difference here?
With this sensor, the NORMAL state with pin 7 connected is shown as 127 – this means that if no motion or warm bodies are detected, the PB7 is being pulled low. Once motion is detected then current flows, lighting up our LED and also showing in our BASIC program output as 255.
Therefore we can make the following adjustment to our BASIC program and look for the change:
If you have ever watched spy or “heist” movies, there is often a scene where the hero, or more usually heroine, has to negotiate a maze of lasers to get to the prize. This is a fancy, Hollywood version of the “break-beam sensor”.
This sensor has two parts, the transmitter and receiver.
On the both the transmission and receiver sides we just have to connect power to the red and ground to the black leads. The receiver has an extra lead which is where the signal is returned.
In normal operation, signal will flow, meaning we get 255 back. If our signal pin is connected to PB7 then the LOSS of signal, IE. the beam is broken, will be detected by 128 dropping from our result, making 127.
In the video I also show a couple more sensors.
One you might have seen on doors and windows, it is a magnetic sensor that makes or breaks a connection when the magnet is attached or taken away. If you want to keep check on a nosey family member or room mate then that might come in handy!
Another way to detect an intruder in your private stash is a light sensor. While ordinarily light and heat would be read using analog readings, you can use them for this purpose by checking for the presence or absence of light, on or off.
Finally, if you want to know if something has been disturbed, there is a kind of sensor that can tell you that. They all work on the principal that a connection can be made or broken by something that can move. My Tilt Sensor has a tiny ball-bearing inside, and while it is in place the connection is made, move it too much and the connection is broken. There are versions using mercury and even highly sensitive versions made with springs that can tell you if there are tiny vibrations.