Arduino MicroPython Control Pump
This tutorial instructs you how to use a 12V pump with an Arduino using MicroPython. We will learn:
- How to connect the pump to the Arduino
- How to write MicroPython code for the Arduino to control the pump's on and off states
This tutorial will enable you to build systems for plant watering, aquariums, or automated water refill stations.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
Overview of 12V Pump
Pinout
A 12V pump usually has two pins.
- Connect the negative (-) pin (black) to the GND on the DC power supply.
- Connect the positive (+) pin (red) to the 12V on the DC power supply.
How to Control Pump
A 12V power supply works perfectly for a 12V pump. To control the pump with an Arduino, connect a relay between them. The Arduino will use the relay to turn the pump on and off. If you're new to relay or MicroPython programming for the Arduino, I recommend checking out the below tutorial:
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
Arduino MicroPython Code
The code below turns the pump ON for five seconds and OFF for five cycles.
Detailed Instructions
Here’s instructions on how to run the above MicroPython code on Arduino with Thonny IDE:
- Make sure Thonny IDE is installed on your computer.
- Make sure MicroPython firmware is installed on your Arduino board.
- If you are new to Arduino with MicroPython, see the Getting Started with Arduino and MicroPython.
- Connect the Arduino board to the 12V pump according to the provided diagram.
- Connect the Arduino board to your computer with a USB cable.
- Open Thonny IDE and go to Tools Options.
- Under the Interpreter tab, select MicroPython (generic) from the dropdown menu.
- Select the COM port corresponding to your Arduino board (e.g., COM33 on Windows or /dev/ttyACM0 on Linux).
- Copy the provided Arduino MicroPython code and paste it into Thonny's editor.
- Save the MicroPython code to your Arduino by:
- Clicking the Save button or pressing Ctrl+S.
- In the save dialog, choose MicroPython device and name the file main.py.
- Click the green Run button (or press F5) to execute the code.
- Verify that the pump is working correctly.
Code Explanation
The comments in the Arduino MicroPython code above explain everything.