• Skip to main content
  • Skip to header right navigation
  • Skip to site footer
Retro Game Coders

Retro Game Coders

Retro computer/console game + dev community

  • Home
  • About
  • Blog
  • Retro Resources
    • Retro Gaming Timeline
    • Browser C64 Emulator
    • Best Retro YouTube Channels
    • New Retro Books
    • Raspberry Pi Amiga Emulation
    • MiSTer FPGA Tutorial
    • BMC64 C64 Pi
  • Contact
Home » Hardware

C64 “Security System” / Digital Input (Programming Electronics with the Commodore 64 Part 2)

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.

Remember to be EXTREMELY careful that you get the correct pins connected, and connect with power OFF
Remember to be EXTREMELY careful that you get the correct pins connected, and connect with power OFF

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 POKE 56579,0.

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:

The first pin is being held low by connecting it to Ground
The first pin is being held low by connecting it to Ground

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.

Try putting pin 0 and ground across an LED along with a resistor (220ohm or above)
Try putting pin 0 and ground across an LED along with a resistor (220ohm or above)

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

POKE 56579,0

Now try

POKE 56579,1

and then

POKE 56577,0

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 Sensor
PIR Motion Sensor

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 …

PIR sensor - Motion allows current to flow
Motion allows current to flow

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:

Break-Beam Sensors

Break-Beam Sensor transmitter and receiver
Break-Beam Sensor transmitter and receiver

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.

Beam on, Beam broken
Beam on, Beam broken

More Sensors

In the video I also show a couple more sensors.

Magnetic Door Sensor
Magnetic Door Sensor

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!

Light Sensor
Light Sensor

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.

How do you think those would be implemented? I will leave that as a thought (or practical) exercise for you!

If you like it, share it! (Please - because it really helps)

  • Tweet
Category: HardwareTag: c64, commodore pet, vic20
Previous Post: « Cheap and Easy Arduino C64 Keyboard to USB Adapter
Next Post: Reading C64 Joysticks and Analog Signals (Programming Electronics with the Commodore 64 Part 3) »

Retro Game Coders

Retro computer/console game + dev community by Chris Garrett

  • Facebook
  • Twitter
  • Instagram
  • YouTube

Maker Hacks ・ Geeky Game Master

© Copyright 2021 Chris Garrett

Privacy ﹒ Terms of Service

Return to top