Is there a way to detect (in Python) that a Blinkt! is physically plugged in?


#1

The subject says it all, really! Just wondering if there was a way to detect if a Blinkt! was plugged in or not, meaning that my python script could choose to run, or just exit and not waste cpu cycles.

Chris.


#2

No, I’m afraid, the APA102 are not communicating back.

I guess you could potentially make a small voltage divider on the blinkt from the 5V pin to another pin on the blnkt and read the voltage on the relevant GPIO, but I haven’t tried it, I’m just brainstorming out loud.


#3

Oh well, never mind! Thanks for answering! :)


#4

Actually, I’m not sure why I was focussed on the 5v pin, you could use the 3v3 pin on the header, easily accessible on the bottom.

Here is a simple hack that @gadgetoid suggested that will work a treat asserting whether a blinkt is hooked up or not:

it is using a 10k ohms resistor to couple BCM4 to 3.3V on physical pin 1 (for protection, so you don’t accidentally short the setup by setting BCM4 as an output driven low).

Here’s some example code to poll BCM4:

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
GPIO.input(4)

Note the use of internal pull-down to avoid the pin being in a floating state when the blinkt is not fitted. Thanks to @allo for pointing that solution out.


#5

I think you can set internal pullup/pulldown on the raspberry pi.


#6

That’s right, you could activate the pulldown on that pin, and use a lower value for the resistor, theoretically.

How low you would need to go, and whether (another process on boot) setting the pin as an output driven low could result in damage I’m not sure. Polling in the manner I suggested is a bit clunky to be sure, but is a safe option. YMMV.


#7

actually, good call, the internal pulldown is not strong enough to resist with a 10k value, at least on a Pi3 (but I don’t believe the internal pullup/down vary from model to model). I have amended the code supplied previously to reflect this.