Close

Switch off

A project log for Assistance System for Vein Detection

Using NIR (near infrared) Illumination and real-time image processing, we can make the veins more visible!

myrijamMyrijam 10/14/2017 at 16:320 Comments

Since we always used to turn the Raspberry Pi off just by pulling the plug, which isn't good for the computer and the SD-card system, we applied a switch to the mobile version in the previous log that turns the Raspberry Pi on - and we need a way to turn it off.

Because, since it is running headless, one otherwise would have to ssh into it to tell it to "sudo shutdown -h" - not a working solution for people who just want to use the device as effortless as possible :-)

We decided to use the same switch to turn the Raspberry Pi on as to shut it down safely to keep things simple. 

Additionally to the libraries we needed before (time, GPIO), we now need one to send system calls like the "sudo shutdown -h"...

from subprocess import call    # for shutdown

Since it is good practice to use definitions/variables we declare pin3 to be the one we use for shutdown:

onoff_pin = 3

Then we declare this GPIO to be an input and since we a lazy and want to keep additional electronics to a minimum, we tell it to use the internal pull-up resistors, so the pin has always a defined level and is not "floating":

GPIO.setup(onoff_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)    # define onoff_pin as input, pulldown

When the on/off-Button is pressed, the system should notify the user of shutdown - of course by the buzzer we integrated in the previous build log. Now it should beep for one second, then tell the Raspberry to shut down. We need to declare a method that will be called when the switch is pressed - because we want it to be triggered by an interrupt (like the encoders) and not watch for it in the main loop doing the image processing: 

def shutmedown(channel):
    GPIO.output(buzzerpin, True)                # Buzzer on
    time.sleep(1)                               # for 1 sec
    GPIO.output(buzzerpin, False)               # then off again
    GPIO.cleanup()                              # not really needed, we shut down now anyway
    call("sudo shutdown -h now", shell = True)  # this is the system call to shut down

After we declared the method, we need to configure the interrupt that is triggered as soon as the button is pressed. Since we used a pull-up internal resistor, the state of our GPIO-Pin is always true - until the button is pressed. The level changed from true (high = 3.3 V) to false (low = 0V = GND), meaning we have a falling edge, a drop:

GPIO.add_event_detect(onoff_pin, GPIO.FALLING, callback = shutmedown, bouncetime = 500) #interrupt falling edge to on/off-button, debounce by 500ms

Debouncing is not really necessary, so the 500ms is uncritical. We don't count, just any press on the button will shut the Raspberry down.

We tried it with a spare Raspberry on a breadboard and since it worked straight away, we now include buzzer and on/off key into our prototype as a next step.

Discussions