Measuring AC level using Photon

This is my first attempt at playing with a photon and I’m stuck. I’m trying to measure the level of an AC signal using an analog input and then publish it as a figure between 0 and 4096.

The default (null) voltage on the input pin is 1.65v. This publishes a figure of around 2048 , which is as I would expect - being half of the maximum value.

When I apply an AC signal at 50hz of any voltage with a peak of up to 3.3v the published reading simply fluctuates around the 2048 figure. Hence, I assume that I’m just measuring an average value.

Can anyone give me an idea of how I get around this using coding, or do I need to ‘DC’ the signal being measured?

Thanks for your help.

The code I’m using is:

int currentvalue = 0;

void setup() {

// Sample loop:

void loop() {
currentvalue = analogRead(A0);

If you are measuring AC like this you’ll only ever get a snapshot of the sine wave at an unpredictable point in time, so no surprise that you are seing nothing but fluctuations.

You should also know how the ADC and analogRead() on the Photon work.
e.g. analogRead() takes a number of consecutive measurements and hands you back the average. Also each individual measurement takes some time, so the reading you get with the code above will also always give you the average of some time frame of the AC wave.

1 Like

It is possible to read an AC (mains) current converted into a voltage varying between 0 and 3.3V max using an analogRead() but as @scruffr has pointed out you have to take some care about how you do it. First, you need to sample at least 5 full cycles of the “sine” wave. I have put sine in inverted commas because depending upon the load (say with a switched charger) you may be trying to measure something more like a sawtooth at 500Hz on top of a 50Hz square wave. To measure this accurately you need to sample at 1000Hz. Also, you want to measure RMS current so you need to take the measurements 0-4095 and then calculate. There are numerous sites where you can find how to do this. Lastly, you will want to stop interrupts while you measure the current to avoid the precise timing of readings being impacted.

1 Like

@Miltonfest, I assumed you were not talking about mains AC :astonished:
If you are in deed thinking of measuring mains voltage:

Don’t do it unless you exactly know what you are doing and you are allowed to do so. Otherwise you may run the risk of serous harm and may lose insurance coverage in case of problems


@ScruffR Sound advice. @Miltonfest Indeed you need to be extremely careful, I assumed you were talking about taking the output from an AC sensor.

1 Like

Thank you for your responses, @Armor and @ScruffR. I appreciate the time taken.

Just to be clear, I’m only measuring a max of 3.3v at 50hz. I’m a retired electronics engineer just having a dabble with programming a photon.

So, it seems I need to take a look at increasing sample rates. I’ll have a search online at how to do that, unless anyone can point me in the right direction…? I’ll calculate the rms values once I’ve resolved the sampling.


1 Like

It looks like you’ll be using a CT with Burden Resistor.
Open Energy Monitor is a great resource and would be a good starting point.

You can Google emon arduino and find days worth of reading that a E.E. would love, plus a lot of code examples. Not many mods required for a Photon.


Thanks for the info, @Rftop . I’ll take a look and post back details of how I get on.


Hello Miltonfest,

Did you have any luck in reading an AC signal on a photon? Can I ask how you finally decided to do it?

I have been trying to figure out if its possible to create an AC signal from the photon to run through a soil moisture sensor (watermark SS200) and take a reading.



@Patrick, welcome to the Particle Forum :smiley:
I’ve used many different soil moisture sensors (including the watermark SS200).
For me, a Capacitive Soil Moisture sensor w/ I2C interface was much easier to implement with Particle Devices and provided the best long term results.

Thanks for the information, this is my first dive into electronics so I like easy. My goal is to bury sensors up to 3.5’ deep so they need to be weatherproof and the watermark seemed to be the ones that are already set up for that. I don’t mind (but would prefer not to) changing the sensors once a year, but they have to last the whole season long.

I’ll bone up on I2C and take another look at capacitive sensors. Are there any you would recommend?


The $22 Rugged version (coated in epoxy resin and protected by adhesive lined heat shrink) of this Sensor is a good starting point.