Raspberry Pi Current SensorAfter constructing a solar panel and battery system to power a Raspberry Pi, I wanted to find a way to monitor how the battery and charger was performing.  There are several ways to accomplish this task, usually involving a voltage sensor, a current sensor, or both.

A voltage-based sensor can be used to monitor the charge of the battery.  Over a typical day, the battery voltage should rise as the solar panel charges the battery, and then discharge at night while the Raspberry Pi consumes power from the battery.  A common technique for monitoring voltage is to use a voltage divider.  If appropriate resistances are chosen for the voltage divider circuit, the drain on the battery from the sensor is negligable.

Another option is to use a current sensor.  A current sensor placed off of one of the battery terminals will measure how much current is flowing into or out of the battery.  If you would prefer it in terms of power, the power being supplied or consumed from the battery can then be calculated using the following equation:

P = I x V


P is the instanteous power, measured in watts;

V is the potential, or voltage, measured in volts; and

I is the current, measured in amperes (a.k.a amps).

I chose an Allegro MicroSystems ACS712 current sensor, which can be found on eBay for $2 – $4 each.  The one I selected was rated for +/- 5 amps.  Rather than use batteries or another wall adapter to power the current sensor, I chose to connect it to the Raspberry Pi using an Adafruit Pi Cobbler cable.  Adafruit also sells several prototyping circuit boards, and I purchased one so I could mount the cable, analog to digital converter, several resistors, and the current sensor.  The one I happened to purchase was sized to fit in an empty mint tin (more Altoids, anyone?).

Adafruit Pi CobblerMy first step was to assemble the Adafruit Pi Cobbler.  The instructions provided by Adafruit were excellent, as usual and I had no trouble with this assembly.  Since the Raspberry Pi uses a digital input, I needed to convert the analog sensor value into a digital value.  For this, I chose the MCP3008 chip, also provided by Adafruit.  In short order, I had the Raspberry Pi sensing the resistance changes by using a potentiometer as a simulated analog value.  To get the Raspberry Pi to sense the input, I had to install some software on the Pi.  I followed the instructions from this Adafruit site, but did not install alsa-utils or mpg321 since I’m not using the Rapsberry Pi to control the volume as the tutorial explains.  I also had to run “sudo apt-get update” first before installing python for it to install correctly.

Quickly I found I had another problem.  “easy_install” was not found on my Pi, but was required by the setup instructions.  I found that I needed to run “sudo apt-get install python-setuptools” to get “easy_install.”  (By the way, I submitted this as a suggested improvement to Adafruit, and they updated their web site within about 24 hours – impressive!)  I then copied the sample code from Adafruit, pasted it into a text file and renamed it readsensor.py.  I then used Cyberduck to SFTP it to a new directory I created on my Pi.  I made it executable by running “sudo chmod +x readsensor.py” while viewing that diretory.

Installing PythonThe next step was simply to replace the potentiometer with the current sensor.  Unfortunately, it is not as simple as just swapping out the potentiometer for the current sensor.  The Raspberry Pi uses 3.3 volts DC and the ACS712 current sensor uses 5 volts DC.  I needed a way to convert the 5 volt output of the current sensor down to something with a maximum of 3.3 volts.  The solution to this was very simple.  I used a voltage divider.  A quick check on this web site told me that I should use roughly a 51k resistor and a 100k resistor for 5 volts DC in and 3.3 volts DC out.  After digging through the Rusty Nail Workshop parts bins, I was able to find some suitable resistors that were close enough to these values.  A quick check on the breadboard showed I had 3.315 volts out.  Perfect.

Prototype Current SensorAfter solding everything up to the prototyping circuit board, I connected the voltage reference pin of the analog to digital converter to the Raspberry Pi’s 3.3 volt DC supply.  I then measured the open circuit current (which was zero since nothing was connected to the current sensor) and found the voltage output was exactly half of the 3.3 volt maximum needed by the Raspberry Pi.  The voltage divider was working perfectly.

A few more tweaks here and there and I had the Pi collecting current sensor data.  I found I needed to create an adjustment factor to help calibrate the current sensor.  It was reasonably accurate when the current was around 150 mA, which was suitable for my purposes.  To make sure I eliminated as many errors as possible, I set up some code to average some sensor readings.  If you would like to use my Python code, feel free to download it here.  Just remember to rename it with a .py extension so it will act as a python script.

I wanted to collect the current sensor data so I could see trends over time.  An instanteous reading is nice, but I wanted to make sure current (or power) was flowing into the battery during the day.  Better yet, I wanted to know how much current was flowing in during the daylight hours.  Was the battery charging a little or a lot?

Cosm InstallCosm.com allows you to upload sensor data (for free!) and will graph it for you.  To get started, I found a tutorial on uploading data from Adafruit (where else?).  Of course, the tutorial was excellent.  (Notice a pattern here?)  I did have a problem since I did not have eeml installed on my Pi, so I had to run “sudo git clone git://github.com/petervizi/python-eeml.git”.  I then changed directories to the eeml folder by typing “cd python-eeml/” and I installed it by typing “sudo python setup.py install”.  After receiving another error about missing lxml, I realized I should have run “sudo apt-get install python-lxmlbefore installing eeml.

After everything was installed, and the software was appropriately edited with my Cosm key and id, I had data uploading and visible on the web site!  If you would like to view my live current sensor data, you can visit the live Cosm stream or view it on the graph below.  Unfortunately, I’m also having some trouble with the sensor data uploading continuously, so there may be some gaps in the data as I work through this problem.  When it’s running, data will be shown below.

The last step was to make the python script run when the Raspberry Pi booted.  For this step, a Google search led me back to Adafruit (by now I should have just skipped Google and gone their as my first step).  Using the instructions here, I was able to make the script boot on startup.  You can download the script I used here.  I did notice that the sensor stopped uploading after about 10 minutes, so as an experiment, I set up a crontab job to start the service every five minutes.  This way, at the most, I’d only lose about five minutes of data if the service suddenly stopped.  I later determined that although this was a potential solution, this wasn’t a good solution, so I modified the python script to simply take one reading every 30 seconds.  I then caused it to ignore any errors uploading to Cosm, so hopefully this will solve the problem where the script stops responding.

After letting it run for a day, I was surprised to see over 2 amps of current flowing in during the peak daylight hours.  During the evening hours when the sun had gone down, I saw a current draw of approximately 171 mA.  I used a multimeter to verify the current consumption and then corrected my adjustment factor in the software to make the data match the multimeter.  As a secondary data point, a quick check on the solar panel specifications indicates that a little over 2 amps is the maximum current the panel will produce, so it is in the right ballpark!  Now I just need to add a voltage divider to measure the battery voltage.  I also found out later that adding an amplifier may give me better resolution for low current sensing.  That’s a future project.

There you have it!  A Raspberry Pi which is solar powered, runs 24 hours a day, 365 days a year AND which provides some energy data from a Web Energy Logger to me as a web server, uploads weather data from a Davis Vantage Pro weather station, and monitors its own solar panel/battery performance while uploading data to Cosm.

15 comments on “Raspberry Pi Solar Charger Current Sensor

  • paul

    Cool project 🙂
    Really nicely written too.

    Just miss the full circuit you used that would be handy for anyone that wants to build it to like me.
    I would be really grateful if you could post it or email it to me.

  • paul

    Very nice blog, I will be attempting to replicate it soon. As I am not only an electronics noob, linux noob and a python noob is it ok if I post here for help?

    • Alligator

      Sure, feel free. Hopefully others can learn from your questions.

  • paul

    Opps first mistake i bouht the ACS712 chip without the board. I realise i can still use it but how am i going to connect my beefy battery cables, carrying up to 20A to it…? Bigger solar panels coming soon!
    i will need somekind of termination block to connect. I bought the 20Amp version by the way. The little jumper leads on my bread board would surley melt with 20A going through them.
    I realise i should move it form a bread board after testing.

    I also have a question about the 3.3v VREF.

    I understand (i think) what a voltage divider is and why you have it. BUT isnt the VREF just for refeference and not for providing power?

    If it is for reference then you could use the PI’s 5v instead of 3v without effecting the PI and no longer need the voltage divider.

    I apologise if i have missed the obvious.

    Any valuable time you spend putting me right is greatly appreciated.

    • Alligator

      Yes, 20A is quite a bit of current and more than I’d want to run through a bread board. I used the voltage divider because the current sensor board and the Raspberry Pi board were at different voltage levels. If I pushed a 5 volt signal into the Raspberry Pi board, I could destroy the Pi. Thus, I needed to scale it down to something in the range of 0 – 3.3 VDC.

      The Raspberry Pi’s power (5V and 3.3V) and ground pins are not GPIOs. The GPIOs on the Pi need to protected against voltages higher than 3.3 VDC.

  • Paul Haynes

    Ok all working except
    i have connected the acs712 amp terminals to my battery feed from the solar charger.
    i cut the positive wire and put it in inbetween.
    But the power does not flow through the acs712…
    From looking at the diagram and googling the wiring i believe i have done it correctly.

    any ideas

    Also i get 4.5 V out of my PI not 5v so my voltage divered is a bit off
    I get 2.25 out of acs712 and 1.5 after divider. im sure i can accomodate this in the programming

    • Alligator

      I’m not sure what could be causing your problem. If you are getting a reading somewhere between 0 and 3 VDC from your current sensor, then it appears that it is sensing the current. Can you try putting in a known value (one you can measure first with a multimeter) and testing the output? In the software, I had to try a number of things to get a reading that appeared to match my multimeter, so it is possible you will have to address it with the programming.

  • Amish

    Hey, if you are using the ACS712, couldn’t you use the setup as mentioned in application 4, and avoid the need for a voltage divider?
    It is linked here: https://lh5.googleusercontent.com/-X3qXRzZpXaQ/UvBF88ruMOI/AAAAAAAAAgA/aFjVGk3pUb0/w506-h328/Application4.png

    Or is this the same as a voltage divider?

    • Alligator

      Yes, it looks like this will output the 3.3 volts necessary for the Pi. It may be worth a try!

  • Jordan

    Hi, thanks for the information, I was wondering how you got the current sensed equation:

    current_sensed = (1000.0 * (0.0252 * (current_reading – 492.0))) – correction_factor

    And why did you set the correction factor to 94.

    Thank you

    • Alligator

      It has been a long time since I came up with that equation. I believe I used a multimeter and measured different values. Then I made the equation to fit the values I had measured. You might want to also check the data sheet for the current sensor to see if they give any recommended equations.

  • dinesh


    I working on same project but how to measure 230V from main line in raspberry pi.

  • Mark


    I have the ACS712 from ebay: https://www.ebay.com.au/itm/ACS712ELC-20A-20A-Current-Sensor-Module-Ammeter-Arduino-Pi-Hall/132283768812

    Have wired up the rest of the project on the breadboard – I’m not 100% sure on where the 100k and 51k resistors go plus the black/grey and red wires (to the potentiometer) in the arduino project. I’m figuring black > GND..as for the others?

    I’ve looked up voltage dividers at: https://learn.sparkfun.com/tutorials/voltage-dividers/all – still lost…what connects to the VCC and OUT pins on the ACS712.

    Many thanks in anticipation!

    • kfredrick

      It was quite a while ago that I set this up, but if I recall correctly, I used the voltage divider to get the output from the current sensor down to a value that wouldn’t burn out the input pin from the Raspberry Pi. The current sensor outputs 5 volts DC, and the Pi accepts only a 3.3 volt DC input. Your reference to voltage dividers at Sparkfun is correct, and the first circuit image on the left side of that page should work. I’d use the current sensor output as the Vin (which if it is 5 volts is then being lowered by the resistors down to 3.3 volts at Vout). R1 would be the 51k resistor, and R2 the 100k resistor. You can check this with their calculator on their page (5 volts in, R1=51000, R2=100000, and Vout calculates to 3.31 volts). Good luck, I hope everything turns out well with your project!

Comments are closed.