Button Raspberry Pi


I´m a beginner in using the raspberry pi. I am trying to set up a button to turn on an image and have a black screen, if the button is not pushed.
To start with simple Code, I startet with changing between two color:

black = button not pushed
red = button pushed

BUT always when the button is bushed a flickering between both colors starts. What did I do wrong? I believe the wiring is right, because it is very simple (GND & GPIO 17).

import processing.io.*;

void setup() {
// set pin 4 as an input:
GPIO.pinMode(17, GPIO.INPUT);

void draw() {
if (GPIO.digitalRead(17) == GPIO.HIGH) { // if the pin is HIGH, the button isn’t pressed

// set the background to black:
background(0, 0, 0);

} else { // if the button is pressed:

// set the background to red:
background(255, 0, 0);


Could you draw a schematic of your wiring nonetheless?

No R-pi here to test. One approach is to update only when you detect a change of button state. You continuosly check if the button is pressed. If it changes to pressed, update your memory field, update your background and keep checking. If you detect a change, the button is disengaged, you update your memory field, you change the background, and you keep checking.

The memory field could be something as simple as a boolean variable in the global scope.

Another approach is to use noLoop()/redraw() approach. Check the reference to understand their implementations.


Hi @EllenBogen - using physical buttons was just covered in a tutorial on our website, have a look here: https://pi.processing.org/tutorial/visual-synth/

Perhaps using GPIO.INPUT_PULLUP helps!


Thank you so much. The PULLUP command worked.

haha, that was too easy.
That’s ok if you are a beginner, but have you ever think about controlling raspberry pi using WhatsApp?
Try to do the same light experiment by sending a command to raspberry pi using WhatsApp.
You can take help from this Control Raspberry pi with whatsapp