# Push button or other button from python

**URL:** <https://in-thread.sonic-pi.net/t/push-button-or-other-button-from-python/3162>\
**Category:** Support, Help & Resources\
**Created:** [January 16, 2020, 3:55am UTC](https://in-thread.sonic-pi.net/t/push-button-or-other-button-from-python/3162 "2020-01-16T03:55:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fer](https://avatars.discourse-cdn.com/v4/letter/f/90ced4/32.png) [@Fer](https://in-thread.sonic-pi.net/u/Fer)\
**Post date:** [January 16, 2020, 3:55am UTC](https://in-thread.sonic-pi.net/t/push-button-or-other-button-from-python/3162/1 "2020-01-16T03:55:21Z")

</div>

## Hi How could you do that from python and using a push button, you play a note or chord of sonic pi This code is to turn on a LED (if it works):

import RPi.GPIO as GPIO  
import time

GPIO.setmode(GPIO.BCM)  
GPIO.setup(24, GPIO.IN, pull\_up\_down = GPIO.PUD\_UP)  
GPIO.setup(23, GPIO.OUT )

## while True: input\_state = GPIO.input(24) if input\_state == False: GPIO.output(23, True) # Enciende el LED print(‘Botón pulsado’) time.sleep(0.3) GPIO.output(23, False) # Apaga el LED

## and this plays a note from a python script (it also works):

from subprocess import call  
call([“sonic\_pi”, “play :E5”])

* * *

when i add this to the led code it doesn’t work  
Thanks for your attention  
thanks google translate xd

---

<div class="post-metadata">

**Author:** ![perpetual\_monday](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/perpetual_monday/32/955_2.png) [@perpetual\_monday](https://in-thread.sonic-pi.net/u/perpetual_monday)\
**Post date:** [January 16, 2020, 5:03am UTC](https://in-thread.sonic-pi.net/t/push-button-or-other-button-from-python/3162/2 "2020-01-16T05:03:32Z")

</div>

Hi @Fer, welcome!

I recommend using [OSC](https://sonic-pi.net/tutorial.html#section-12) to communicate the notes to play from Python to Sonic Pi. See this thread for an (similar) example and more detail: [Link sonic pi to Python](http://in-thread.sonic-pi.net/t/link-sonic-pi-to-python/2938/5)  
You can adapt that code to send note values and play them in Sonic Pi - let us know if you need more help.

---

<div class="post-metadata">

**Author:** ![Fer](https://avatars.discourse-cdn.com/v4/letter/f/90ced4/32.png) [@Fer](https://in-thread.sonic-pi.net/u/Fer)\
**Post date:** [January 16, 2020, 5:22am UTC](https://in-thread.sonic-pi.net/t/push-button-or-other-button-from-python/3162/3 "2020-01-16T05:22:31Z")

</div>

Thank you very much for answering, I will try

---

<div class="post-metadata">

**Author:** ![nlb](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nlb/32/997_2.png) [@nlb](https://in-thread.sonic-pi.net/u/nlb)\
**Post date:** [January 16, 2020, 10:39am UTC](https://in-thread.sonic-pi.net/t/push-button-or-other-button-from-python/3162/4 "2020-01-16T10:39:07Z")

</div>

hi @Fer

to display code into posts more friendly, puts code between `````

---

<div class="post-metadata">

**Author:** ![Fer](https://avatars.discourse-cdn.com/v4/letter/f/90ced4/32.png) [@Fer](https://in-thread.sonic-pi.net/u/Fer)\
**Post date:** [January 16, 2020, 5:32pm UTC](https://in-thread.sonic-pi.net/t/push-button-or-other-button-from-python/3162/5 "2020-01-16T17:32:56Z")

</div>

I’ll keep it in mind
