Here’s an Arduino sketch to control a 3V DC pump using a push button to start and stop the pump. Additionally, it includes two indicators (green and red LEDs) to signify the status of the pump (running or stopped). This project is intended for the kids, however parents will be required to help them.
Although the project is the kids, the sketch includes the usage of Arduino Interrupt. We have dedicated articles explaining What is Interrupt and How Interrupt Works. This function is used to make the system responsive. If we simply write the code, the button press will be erratic.
The sketch sets up a simple control system where pressing a button toggles the state of a 3V DC pump and updates the state of two LEDs to indicate whether the pump is running or stopped. Using interrupt-based button detection ensures immediate responsiveness to button presses, providing a smooth user experience.
---
Required Components For This Project
- 1x Arduino Nano (or another Arduino UNO)
- 1x Breadboard
- Few Jumper wires
- 1x 3V Mini water pump
- Water pump pipes
- 1x PN2222 transistor
- 3x 330 Ohm resistors
- DC power source, such as battery
- Computer with Arduino IDE installed
The connection to the 3V mini pump will be like that we have described in Arduino 3V DC Motor Control. You can read that article for information on transistors and diodes for this kind of motor.
Connection, Simulation and Sketch
Here is the simulation of the project:
Here are the connection details:
Arduino’s PIN 5 will be connected to the pump. Instead of a direct connection to the pump, a transistor has been used.
Arduino’s PIN 2 will be connected to the push button.
Arduino’s PIN 8 will be connected to the green LED indicator.
Arduino’s PIN 9 will be connected to the red LED indicator.

Here is the sketch:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | // Define pin numbers for components #define PUMP_PIN 5 // Pin connected to the 3V DC pump #define BUTTON_PIN 2 // Pin connected to the push button #define GREEN_LED_PIN 8 // Pin connected to the green LED indicator #define RED_LED_PIN 9 // Pin connected to the red LED indicator // Define variables to store pump state and LED states volatile bool pumpRunning = false; bool greenLedState = LOW; bool redLedState = LOW; void setup() { // Initialize pins pinMode(PUMP_PIN, OUTPUT); pinMode(BUTTON_PIN, INPUT_PULLUP); // Use internal pull-up resistor pinMode(GREEN_LED_PIN, OUTPUT); pinMode(RED_LED_PIN, OUTPUT); // Attach interrupt to the button pin attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonPressed, FALLING); // Turn off pump and LEDs initially digitalWrite(PUMP_PIN, LOW); digitalWrite(GREEN_LED_PIN, LOW); digitalWrite(RED_LED_PIN, LOW); } void loop() { // Update LED states based on pump state digitalWrite(GREEN_LED_PIN, greenLedState); digitalWrite(RED_LED_PIN, redLedState); // Check if the pump is running and update LED states accordingly if (pumpRunning) { greenLedState = HIGH; // Turn on green LED redLedState = LOW; // Turn off red LED digitalWrite(PUMP_PIN, HIGH); // Turn on the pump } else { greenLedState = LOW; // Turn off green LED redLedState = HIGH; // Turn on red LED digitalWrite(PUMP_PIN, LOW); // Turn off the pump } } // Interrupt service routine for button press void buttonPressed() { // Toggle pump state pumpRunning = !pumpRunning; } |
You’ll get the above code from my GitHub repository too. Here is the link.
Explanation of the Sketch
These lines define the pin numbers for the components connected to the Arduino board:
1 2 3 4 | #define PUMP_PIN 8 // Pin connected to the 3V DC pump #define BUTTON_PIN 2 // Pin connected to the push button #define GREEN_LED_PIN 5 // Pin connected to the green LED indicator #define RED_LED_PIN 6 // Pin connected to the red LED indicator |
1 2 3 | volatile bool pumpRunning = false; bool greenLedState = LOW; bool redLedState = LOW; |
pumpRunning: This variable stores the current state of the pump. It’s declared as volatile because it’s modified inside an interrupt service routine.greenLedState and redLedState: These variables store the current states of the green and red LEDs, respectively. Initially, both LEDs are set to LOW (off).
1 2 3 4 5 6 7 8 9 10 11 12 | void setup() { pinMode(PUMP_PIN, OUTPUT); pinMode(BUTTON_PIN, INPUT_PULLUP); // Use internal pull-up resistor pinMode(GREEN_LED_PIN, OUTPUT); pinMode(RED_LED_PIN, OUTPUT); attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonPressed, FALLING); digitalWrite(PUMP_PIN, LOW); digitalWrite(GREEN_LED_PIN, LOW); digitalWrite(RED_LED_PIN, LOW); } |
pinMode(): Configures the pins as inputs or outputs.attachInterrupt(): Attaches an interrupt to the button pin (BUTTON_PIN). When the button pin transitions from HIGH to LOW (FALLING edge), the buttonPressed() function is called.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | void loop() { digitalWrite(GREEN_LED_PIN, greenLedState); digitalWrite(RED_LED_PIN, redLedState); if (pumpRunning) { greenLedState = HIGH; redLedState = LOW; digitalWrite(PUMP_PIN, HIGH); } else { greenLedState = LOW; redLedState = HIGH; digitalWrite(PUMP_PIN, LOW); } } |
Continuously updates the states of the green and red LEDs based on their corresponding variables (greenLedState and redLedState).
Checks the pumpRunning variable to determine whether to turn on or off the pump and LEDs accordingly.
buttonPressed() Function (Interrupt Service Routine):
1 2 3 | void buttonPressed() { pumpRunning = !pumpRunning; } |
This function is called whenever the button is pressed due to the attached interrupt. Toggles the pumpRunning variable, effectively starting or stopping the pump.