Raspberry Pi Pico - ACS712 Current Sensor

In this guide, we will learn to set up and use the ACS712 30A current sensor with the Raspberry Pi Pico. How much current does your motor pull? Is your LED strip drawing more than the adapter can give? The ACS712 answers these questions with a simple analog voltage, and the built-in ADC of the Pico can read it with no extra chip. We will cover the following details:

Raspberry Pi Pico and ACS712 current sensor module

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico Alternatively,
1×Micro USB Cable
1×ACS712 30A Current Sensor Module
1×Resistor Kit (one 10 kΩ and one 20 kΩ resistor)
1×12V DC Device (e.g. 12V DC Motor)
1×Alternatively, 12V DC Device (e.g. 12V LED Strip)
1×12V Power Adapter
1×DC Power Jack
1×Breadboard
1×Jumper Wires
1×Recommended: Screw Terminal Expansion Board for Raspberry Pi Pico

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of ACS712 Current Sensor

ACS712 current sensor pinout

The ACS712 (made by Allegro) measures current with the Hall effect. Inside the chip, the load current runs through a thick copper path. This current makes a small magnetic field, and a Hall sensor next to the copper turns the field into a voltage. The load side and the Pico side are not wired together (the chip has 2.1 kV RMS isolation), and the copper path has only about 1.2 mΩ of resistance, so the sensor barely changes your circuit. It works for both DC and AC.

The chip comes in three versions. They look the same, so read the text on the chip: this guide uses the 30A one (ACS712ELCTR-30A-T).

ACS712 5A ACS712 20A ACS712 30A
Current Range -5 to +5 A -20 to +20 A -30 to +30 A
Sensitivity 185 mV per amp 100 mV per amp 66 mV per amp
ACS712 30A (this guide)
Supply Voltage 5 volts (4.5 to 5.5 volts) - not 3.3 volts
Output at 0 A about half of the supply, near 2.5 volts
Output at +30 A about 4.48 volts
Output at -30 A about 0.52 volts
Bandwidth 80 kHz
Total Output Error about ±1.5% at 25°C
Isolation 2.1 kV RMS

The output is ratiometric. This means the zero point and the 66 mV per amp both follow the real 5 volts supply. On the Pico, the sensor gets its 5 volts from the USB port, and USB is often a bit lower, like 4.9 volts. That is why the code measures the zero point by itself instead of trusting 2.5 volts.

The module has a 3-pin header for the Pico and a 2-pin screw terminal for the load:

  • VCC pin: connect to 5 volts. On the Pico, use the VBUS pin.
  • GND pin: connect to GND (0 volts).
  • OUT pin: the analog output. It goes to an ADC pin of the Pico through a voltage divider (see the wiring part).
  • IP+ and IP- screw terminals (some boards print no label): put them in series with the load. Cut one wire of the load and place the terminal in the gap. When current flows from IP+ to IP-, the reading is positive. Swap the two wires and the sign turns negative.

A small LED on the board turns on when the module gets power.

A note about small currents: the 30A version is not made for tiny loads. On the Pico, one ADC step is about 0.8 mV at the pin, which is about 1.2 mV at the sensor output after the divider, or about 18 mA. The noise of the sensor is bigger than that, so a reading that jumps between about -0.08 and +0.08 A with nothing connected is normal. This sensor is a good fit for loads from about 0.5 A up: motors, lamps, LED strips, heaters and chargers. For small loads, the 5A version gives finer steps.

How the Voltage Becomes Current

At 0 A, the output sits at the zero point. Every amp moves it by 66 mV, up for one direction and down for the other. So the formula is:

  • Current (A) = (output voltage - zero voltage) / 0.066

For example, if the zero point is 2.472 volts and the output reads 2.538 volts, the current is (2.538 - 2.472) / 0.066 = 1.00 A.

Wiring Diagram

The ADC pins of the Raspberry Pi Pico accept 0 to 3.3 volts only, but the ACS712 is powered from 5 volts and its OUT pin can go up to about 4.5 volts. So we do not connect OUT straight to the Pico. Two resistors make a voltage divider that scales the signal to 2/3 of its value: 4.5 volts becomes 3.0 volts, which is safe. The code multiplies the reading by 1.5 to get the real sensor voltage back.

The wiring diagram between Raspberry Pi and Pico ACS712 current sensor

This image is created using Fritzing. Click to enlarge image

ACS712 Module Raspberry Pi Pico
VCC VBUS (pin 40, 5 volts from USB)
GND GND (pin 38)
OUT 10 kΩ resistor to GP26 / ADC0 (pin 31)
(divider bottom) 20 kΩ resistor from GP26 to GND (pin 33)

The load side:

  • 12 volts adapter + (through the DC power jack) to the IP+ screw terminal.
  • IP- screw terminal to the + wire of the 12 volts DC device (such as a DC motor, LED strip or lamp).
  • The - wire of the DC device goes back to the adapter -.

A few points to keep in mind:

  • VBUS gives 5 volts only while the Pico is powered from the USB cable. If you power the Pico in another way, give the sensor its own 5 volts.
  • Never put the 20 kΩ and 10 kΩ resistors the wrong way round. With 10 kΩ on top and 20 kΩ to GND, the pin sees 2/3 of OUT. Swapped, it would see only 1/3, and the code would read the wrong values.
  • The 12 volts circuit does not need to share GND with the Pico. The sensor keeps the two sides apart.

※ NOTE THAT:

Mains AC (110 or 220 volts) can kill. If you are a beginner, practice only with a low voltage DC load like the 12 volts DC device in this guide. The screw terminal and the copper side of the module carry the full load voltage. Never touch them, or any load wire, while a mains circuit is live.

Raspberry Pi Pico Code - ACS712 DC Current

There is no library to install. The code only uses machine.ADC:

  • At the start, it reads the sensor 1000 times while the load is off and saves the average as the zero point.
  • In the loop, it averages 300 readings, turns the result into the sensor voltage (×3.3/65535, then ×1.5 for the divider), and uses the formula above to get the current.
  • It prints the sensor voltage and the current every half second.
""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-acs712-current-sensor """ # Raspberry Pi Pico + ACS712 30A: measure DC current # No library needed: the sensor gives an analog voltage, read with machine.ADC from machine import ADC, Pin import time SENSOR_PIN = 26 # The Raspberry Pi Pico pin GP26 (ADC0) connected to the middle of the 10k/20k divider ADC_VREF = 3.3 # The Pico ADC measures from 0 to 3.3 volts DIVIDER_RATIO = 1.5 # 10k + 20k divider: sensor voltage = pin voltage x (10k + 20k) / 20k SENSITIVITY = 0.066 # ACS712 30A version: 66 mV (0.066 V) per amp SAMPLES = 300 # number of readings averaged for one result adc = ADC(Pin(SENSOR_PIN)) def read_sensor_voltage(count): # Average many readings to reduce noise, then turn the result into # the real voltage on the OUT pin of the ACS712 (before the divider) total = 0 for _ in range(count): total += adc.read_u16() # 0 to 65535 pin_voltage = (total / count) * ADC_VREF / 65535 return pin_voltage * DIVIDER_RATIO # Zero calibration: the load MUST be off now. # With 0 A, the ACS712 output is about half of its 5 V supply, but not exactly, # so we measure the real zero point instead of guessing 2.5 V. print("Calibrating... keep the load OFF") time.sleep(1) zero_voltage = read_sensor_voltage(1000) print("Zero point: {:.3f} V".format(zero_voltage)) while True: voltage = read_sensor_voltage(SAMPLES) current = (voltage - zero_voltage) / SENSITIVITY # amps print("Sensor: {:.3f} V | Current: {:.2f} A".format(voltage, current)) time.sleep_ms(500)

Detailed Instructions

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pi Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Wire the components as shown in the wiring diagram and table. Keep the 12 volts adapter unplugged (or the load switched off) for now.
  • Connect the Raspberry Pi Pico to your computer using a Micro USB cable.
  • Launch the Thonny IDE on your computer.
  • Open Tools Options. On the Interpreter tab, select MicroPython (Raspberry Pi Pico) and the correct port.
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, select Raspberry Pi Pico.
    • Save the file as main.py
  • Click the green Run button (or press F5). The load must still be off, because the first second is used to find the zero point.
  • After the "Zero point" line shows up, plug in the 12 volts adapter so the DC device turns on.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Calibrating... keep the load OFF Zero point: 2.472 V Sensor: 2.474 V | Current: 0.03 A Sensor: 2.470 V | Current: -0.03 A Sensor: 2.473 V | Current: 0.02 A Sensor: 2.589 V | Current: 1.77 A Sensor: 2.536 V | Current: 0.97 A Sensor: 2.531 V | Current: 0.89 A Sensor: 2.538 V | Current: 1.00 A Sensor: 2.533 V | Current: 0.92 A Sensor: 2.471 V | Current: -0.02 A
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

Here, the DC device is a 12 volts DC motor. The first lines show only noise, because no current flows yet. When the motor starts, it pulls a short peak (1.77 A here), then settles near 0.9 to 1.0 A. If you hold the motor shaft with your fingers for a moment, the current goes up. Unplug the adapter and the value falls back near 0 A.

If the current shows a minus sign while the DC device runs, the load wires are in the screw terminal the other way round. Swap them, or just read the number without the sign. If the zero point is far from 2.5 volts (for example, below 2.2 volts), check the divider resistors and make sure VCC is on VBUS, not on 3V3(OUT).

If you name your script main.py and save it to the root directory of the Raspberry Pi Pico, it will automatically run each time the Pico is powered on or reset. Remember to keep the load off at that moment, so the zero point is right.

Measuring AC Current

With AC, the current changes direction many times every second (50 or 60 times, depending on your country). The sensor output swings above and below the zero point, so the average is about 0 A. A single reading or a plain average tells us nothing. Instead, we use the RMS value, which is the number a clamp meter shows:

  • Sample the output again and again for 200 ms. This covers 10 full cycles at 50 Hz, or 12 at 60 Hz.
  • For every sample, take the distance from the zero point and square it.
  • RMS current = square root of (average of the squares) / 0.066

The wiring stays the same. The screw terminal goes in series with one wire of the AC load.

※ NOTE THAT:

Again: mains voltage is dangerous. Only do this if you know how to work with mains safely, use a closed box, and never touch the screw terminal or wires while the power is on. The 30A module is rated for this, but your wires and connections must be too.

Raspberry Pi Pico Code - ACS712 AC Current

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-acs712-current-sensor """ # Raspberry Pi Pico + ACS712 30A: measure AC current (RMS value) # No library needed: the sensor gives an analog voltage, read with machine.ADC from machine import ADC, Pin import time import math SENSOR_PIN = 26 # The Raspberry Pi Pico pin GP26 (ADC0) connected to the middle of the 10k/20k divider ADC_VREF = 3.3 # The Pico ADC measures from 0 to 3.3 volts DIVIDER_RATIO = 1.5 # 10k + 20k divider: sensor voltage = pin voltage x 1.5 SENSITIVITY = 0.066 # ACS712 30A version: 66 mV (0.066 V) per amp WINDOW_MS = 200 # 200 ms = 10 cycles at 50 Hz, 12 cycles at 60 Hz VOLTS_PER_STEP = ADC_VREF / 65535 * DIVIDER_RATIO adc = ADC(Pin(SENSOR_PIN)) def find_zero(count): # Average the output while NO current flows total = 0 for _ in range(count): total += adc.read_u16() return (total / count) * VOLTS_PER_STEP def read_rms_current(zero): # Sample for the whole time window and keep the sum of the squares sum_squares = 0 samples = 0 start = time.ticks_ms() while time.ticks_diff(time.ticks_ms(), start) < WINDOW_MS: diff = adc.read_u16() * VOLTS_PER_STEP - zero # distance from the zero point sum_squares += diff * diff samples += 1 rms_voltage = math.sqrt(sum_squares / samples) return rms_voltage / SENSITIVITY # amps (RMS) print("Calibrating... keep the load OFF") time.sleep(1) zero_voltage = find_zero(1000) print("Zero point: {:.3f} V".format(zero_voltage)) while True: current = read_rms_current(zero_voltage) print("AC current (RMS): {:.2f} A".format(current)) time.sleep_ms(500)

Detailed Instructions

  • Keep the same Pico wiring, and keep the AC load switched off.
  • Paste this code into Thonny, save it to the Pico as main.py, and click Run.
  • Wait for the "Zero point" line, then switch the load on. Look at the Shell:
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Calibrating... keep the load OFF Zero point: 2.469 V AC current (RMS): 0.06 A AC current (RMS): 0.07 A AC current (RMS): 0.44 A AC current (RMS): 0.43 A AC current (RMS): 0.44 A AC current (RMS): 0.06 A
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

Here, a 100 W lamp at 230 volts was switched on and off. It should draw about 100 / 230 = 0.43 A, and the Pico shows 0.43 to 0.44 A. Note that the RMS value is never exactly 0 A with the load off: the noise also gets squared, so about 0.05 to 0.1 A is the floor of the 30A sensor. That is one more reason to use it for bigger loads.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!