I am attempting to make a button that can press a key on my keyboard at a random interval between 1 and 5, and would prefer if the code was written in python. I am really new to this website, and I have a raspberry Pi 0. I also need help finding hardware that will push the button down for me.

  • 1
    what do you mean by "hardware that will push the button down for me" Do you mean a robot hand? – tlfong01 May 5 '19 at 5:13
  • Yes, exactly. Sorry, I could have been more detailed. – Samuel Q May 5 '19 at 12:27
  • Ah yes, I once used a solenoid to press a micro switch. Let me see if I can search my old posts for you. – tlfong01 May 5 '19 at 12:33
  • Awesome, thank you! – Samuel Q May 5 '19 at 13:12
  • 1
    why do you need to use a mechanical device to press a key on a keyboard? – jsotola May 6 '19 at 1:14


How to use Rpi python [Rpi.GPIO] to push a button.


You seemed to have confused with a button with an "actuator" which can be used to push a button, or a key of the keyboard. Let us first see what Wikipedia describes what is an actuator.

Actuator - Wikipedia

An actuator is a component of a machine that is responsible for moving and controlling a mechanism or system, for example by opening a valve. In simple terms, it is a "mover".

An actuator requires a control signal and a source of energy. The control signal is relatively low energy and may be electric voltage or current, pneumatic or hydraulic pressure, or even human power. Its main energy source may be an electric current, hydraulic fluid pressure, or pneumatic pressure. When it receives a control signal, an actuator responds by converting the signal's energy into mechanical motion.

An actuator is the mechanism by which a control system acts upon an environment. The control system can be simple (a fixed mechanical or electronic system), software-based (e.g. a printer driver, robot control system), a human, or any other input.

A solenoid as a actuator

A common actuator is a solenoid. Perhaps I can show you some pictures, followed by a brief description of how the solenoid can be controlled by Rpi python to push a button or a key of the keyboard.

You might get a rough idea of the following video showing how I use Rpi Python to control a solenoid. The solenoid can operate only as fast as 10 times per second.

Youtube video of Rpi python controlling a solenoid

Now some buttons to simulate the keyboard keys, so you don't need to place the solenoids on top of the keyboard. This way the keyboard can be manual or software controlled at the same time, ...

psuh buttons

Big and small solenoids

You can find very small solenoids, as shown in the picture below. But they are not strong enough to press the keys of a keyboard, or its travel distance is too short to completely press down the key.


/ to continue, ...

The paragraphs below are useful to answer a small part of the OP's question. They need to be heavily modified or removed.

Part 1 - Pushing button by solenoid

To push a button using Rpi python is a bit tricky. I once used Rpi python to control a solenoid to push a button. Let me show you some pictures to give a rough idea of how difficult it is to do the job.

First, the button or switches. I assume the following buttons are what you wish to

Part 2 - Pushing button by hand

I am assuming you are pushing the button by hand. There are many good tutorials on this. I would recommend the following two tutorials. Of course you can easily google more.

You can use Rpi.GPIO. It is easier to use GPIO ZERO.

Push button using Python and GPIO - RaspberryPiIVBeginners

Connecting a Push [Button/]Switch with Raspberry Pi - O'Reilly 2013dec11

Connecting a Push [Button/]Switch


Python RPi.GPIO Module Documentation - Python Software Foudation

GpioZero Documantation - Raspberry Pi Foundation

Example Python Rpi.GPIO Push Button Program

# Rpi Python using Rpi.GPIO to read the status of a push button
# Reference - https://www.modmypi.com/download/youtube_workshop_4_button.pdf

from time import sleep
import RPi.GPIO as GPIO

# *** Setup BCM GPIO numbering ***


# *** Setup GPIO pin 10 to input mode with pull up resistor ***

GPIO.setup(10, GPIO.IN, pull_up_down = GPIO.PUD_UP)

# *** Repeat read status of push button (press Ctrl-C to exit program)

while True:
  if (GPIO.input(10) == False ):
    print("Button Pressed")
  • Sorry, I didn't go detailed enough. I need to make a physical button pusher, but the interval that it gets pressed by needs to be controlled by the Pi0 – Samuel Q May 5 '19 at 12:28
  • Ah I see what you mean. I once use a solenoid to push a micro switch, and it works OK. Let me search my junk box later. – tlfong01 May 5 '19 at 12:32
  • There are different types of solenoids, some very powerful to push big heavy buttons. The following yoututbe show my first experiment with a standard toy solenoid. It can push micro switches with any problems. youtube.com/watch?v=hov3hx7tpAE&feature=youtu.be You might like to let mek now if it looks appropriate. – tlfong01 May 5 '19 at 12:41
  • I don't need it to press with much force. From above, I need it to press a mechanical keyboard key. – Samuel Q May 5 '19 at 13:13
  • No problem. The standard solenoid I am showing can press the keyboard key with a problem. You may try smaller solenoids. Also, you don't need to place the solenoid on top of the keyboard. If you only need to press only one key or a couple of keys, you can extend the keyboard 's connecting wires and use external keys or buttons to simulate the keys. If you wish to press many keys, the mechanical setup will be very messy. :) – tlfong01 May 5 '19 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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