I have a raspberry pi 3 model B+ and I have been trying to send a pulse to the small audio jack port, Is this the correct way please?

    import RPi.GPIO as GPIO             
    from time import sleep             
    GPIO.setup(21, GPIO.OUT)           
        while True:  
            GPIO.output(21, 1)         
            GPIO.output(21, 0)          
    except KeyboardInterrupt:          

It doesn't seem to work for some reason. Could someone tell me if I am doing something wrong please?

In case it matters, when I right-click on the small speaker icon next to the clock at the top right of the screen a small menu with only two elements pops up; Analog and HDMI. By default, HDMI is selected, and it has a small checkmark next to it. I tried to switch it to Analog but didn't make a difference. Does it matter whether Analog or HDMI is selected?

EDIT (to add more details) Apologies for my ignorance, I am completely new to this. This little device is connected to another one called an injector. It connects to the injector with a 3.5in jack. What I want to do is to use a raspberry pi to connect to (and control) the injector (instead of using the device in the link above). I was told that a stereo cable (with two male 3.5in jacks) and a python script would work. I now doubt that in the sense that the audio cable shouldn't be plugged into the 3.5 port of the pi. Instead, I should probably take the audio cable and just chop off one of the two male jacks, strip the cables and the connect one of the wires to earth and the other to pin40 (or whatever pin the python script indicates) Does it make sense?. Happy to receive any comments, let me know if I am talking rubbish...

  • That sounds awkward, but perhaps edit your question to explain what you want to do - your ultimate objective, if you will - or just explain why you want to feed a GPIO output into the audio jack. IOW, there may be other methods to get there.
    – Seamus
    Feb 8 at 7:46
  • @Seamus, thanks so much, I just edited my question, hope it makes more sense now.
    – Aenaon
    Feb 8 at 13:50
  • You're getting warmer: It now sounds do-able, but we still need more information. It sounds like your little device is a foot switch, and when you press the foot switch, it supplies power to the injector... is that about right? Also, your objective is to power the injector with the RPi instead of the foot switch? If so, it would be really helpful if you could tell us how much voltage and current the injector uses. But don't let me lead you down the path... if my assumptions are incorrect, it's in your interest to clarify that now.
    – Seamus
    Feb 8 at 17:14
  • Thanks a lot Seamus. We have the injector and we do not have the foot switch. The aim is to have a raspberry pi connected to the injector and I was also told to use the audio cable to do that. I know for certain that these instructions are correct because that person has already done it but he has moved on and left us with these directions. However I believe one end of the male-male audio cable should be stripped and connected directly to the pins. Now the injector will be triggered by sending GPIO.output(pin_num, False) and then after some time t GPIO.output(pin_num, True) ...
    – Aenaon
    Feb 8 at 19:41
  • (cntd from above) ... from the raspberry pi. I think the python code isnt going to be a problem, it has been given to me by that person who has moved on. Inside the python script pin num 40 is specified with the commands below pin_num = 40, GPIO.setmode(GPIO.BOARD), GPIO.setup(pin_num, GPIO.OUT), GPIO.output(pin_num, True). I believe it is the wiring I have to sort-out. I hope it makes sense! I also suspect the male-male audio cable ended up to me by some misunderstanding. It is meant to be naked on one end and male on the other If I am right. Thanks again!
    – Aenaon
    Feb 8 at 19:42

1 Answer 1


GPIO 21 is not connected to the audio which is normally controlled by PWM (GPIO 40, 41).

Even if you got the correct pins it is unclear what would happen as the audio jack has a low pass filter and is capacitively coupled.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.