Jump to content
 
  • entries
    7
  • comments
    6
  • views
    11,207

About this blog

Using a Raspberry Pi to control model railway layout functions

Entries in this blog

7. Basic inputs

So far we have looked at outputs to control LEDs. Now let's look at inputs. There are two ways to detect an input.     In Method 1: GPIO is HIGH when button is down. GPIO is LOW when button is up. In Method 2: GPIO is LOW when button is down. GPIO is HIGH when button is up. Either method can be used depending on the application.   Here's how it looks on the breadboard. Method 1 is on the left, attached to GPIO24 (pin18). Method 2 is on the right, attached to GPIO25 (pin

DavidB-AU

DavidB-AU

6. 2-aspect signal control from a single GPIO pin

In the previous tutorial we saw two different ways to control an LED and how the same state can have the opposite effect depending on how the circuit is designed. We can use this to our advantage by driving both LEDs from the same GPIO pin. This requires a little more electronics.   This does two things. - When pin 18 is HIGH (True), it triggers the NPN transistor to power the red LED but prevents powering the green LED because the anode and cathode are both at 3.3V. - When pin 18 is LOW (

DavidB-AU

DavidB-AU

5. Another way to drive an LED (or two)

An earlier tutorial showed one way of driving an LED, connecting the anode side to the 3.3V rail and the cathode side to GPIO. As mentioned, the default state of the GPIO output is HIGH (3.3V) to setting it LOW (0V) allows power to flow through the circuit. Alternatively, the anode of the LED can be connected to GPIO and the cathode to GND. This changes the way to drive it, HIGH (True) to turn on, LOW (False) to turn off. GPIO.output(x, False) to turn on, GPIO.output(x, True) to turn off.  

DavidB-AU

DavidB-AU

4. Simple 2-aspect signal control

We use the same electronic set up as the previous tutorial, but now pretend it is a simple 2-aspect colour light signal. Instead of LEDs on the breadboard, there could be leads out to a real signal on the layout.   We want to be able to control this "signal" remotely by pressing the appropriate keys on the keyboard. Unfortunately this requires importing some more packages and adding more code to detect when a key is pressed. (You don't necessarily need to understand how this bit works.)  

DavidB-AU

DavidB-AU

3. Adding a second LED

Now let's add a green LED adjacent to the red one and connect it to GPIO25 (pin 22).   Now create a new file called ledtest2.py and enter the following: import RPi.GPIO as GPIOimport time# Set up GPIO25 (pin 22) as outputGPIO.setup(22, GPIO.OUT)while 1: # Turn green LED on GPIO.output(22, False) time.sleep(1) # Turn green LED off GPIO.output(22, True) time.sleep(1)   Run the program as root. pi@raspberrypi ~$ sudo python ledtest2.py   If all goes well, the green LED will flas

DavidB-AU

DavidB-AU

2. Driving an LED

Now let's set up a simple circuit with a resistor and a red LED. The resistor is between the 3.3V rail and the anode (long leg) of the LED. The cathode of the LED is connected to GPIO24 (pin 18).   The value of the resistor isn't critical but should be in the range of 300R-1K. I used 470R because I had plenty to hand. This is what it looks like on the breadboard.   Now to control the LED from the RPi. GPIO must be run as root so after the prompt (pi@raspberrypi ~$) type sudo python. pi@

DavidB-AU

DavidB-AU

1. Basic set up

This is the first of hopefully a series of tutorials on using a Raspberry Pi to do what modellers often do with more mundane devices such as switches. These are aimed at the modeller rather than the serious hardcore programmer or electronics hobbyist, but does assume a basic knowledge of electronics and assumes you have already set up your RPi and installed the RPi.GPIO package. I won't go into that here - if you have any trouble see the Raspberry Pi or Adafruit forums.   Note that my RPi is

DavidB-AU

DavidB-AU

×
×
  • Create New...