# Button Raspberry Pi

**URL:** https://discourse.processing.org/t/button-raspberry-pi/1823
**Category:** Processing for Pi
**Created:** [July 16, 2018, 5:30pm UTC](https://discourse.processing.org/t/button-raspberry-pi/1823 "2018-07-16T17:30:35Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![EllenBogen](https://avatars.discourse-cdn.com/v4/letter/e/aca169/32.png) [@EllenBogen](https://discourse.processing.org/u/EllenBogen)
#### Post date: [July 16, 2018, 5:30pm UTC](https://discourse.processing.org/t/button-raspberry-pi/1823/1 "2018-07-16T17:30:36Z")

</div>

Hi,

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).

Code:  
import [processing.io](http://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);

```

}  
}

---

<div class="post-metadata">

### Author: ![colouredmirrorball](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/colouredmirrorball/32/234_2.png) [@colouredmirrorball](https://discourse.processing.org/u/colouredmirrorball)
#### Post date: [July 16, 2018, 6:04pm UTC](https://discourse.processing.org/t/button-raspberry-pi/1823/2 "2018-07-16T18:04:50Z")

</div>

Could you draw a schematic of your wiring nonetheless?

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [July 16, 2018, 10:16pm UTC](https://discourse.processing.org/t/button-raspberry-pi/1823/3 "2018-07-16T22:16:09Z")

</div>

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.

Kf

---

<div class="post-metadata">

### Author: ![gohai](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gohai/32/25_2.png) [@gohai](https://discourse.processing.org/u/gohai)
#### Post date: [July 17, 2018, 2:31am UTC](https://discourse.processing.org/t/button-raspberry-pi/1823/4 "2018-07-17T02:31:04Z")

</div>

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/](https://pi.processing.org/tutorial/visual-synth/)

Perhaps using `GPIO.INPUT_PULLUP` helps!

---

<div class="post-metadata">

### Author: ![EllenBogen](https://avatars.discourse-cdn.com/v4/letter/e/aca169/32.png) [@EllenBogen](https://discourse.processing.org/u/EllenBogen)
#### Post date: [July 17, 2018, 8:34am UTC](https://discourse.processing.org/t/button-raspberry-pi/1823/5 "2018-07-17T08:34:16Z")

</div>

Thank you so much. The PULLUP command worked.

---

<div class="post-metadata">

### Author: ![Afflospark](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@Afflospark](https://discourse.processing.org/u/Afflospark)
#### Post date: [April 14, 2019, 6:58pm UTC](https://discourse.processing.org/t/button-raspberry-pi/1823/6 "2019-04-14T18:58:40Z")

</div>

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](https://www.experthoot.com/how-to-control-a-raspberry-pi-using-whatsapp/)
