Help on programming MEGA with home energy monitoring

Hello all,

i've been searching and searching but cannot find a good example code what will fix my problem.

I'm building a project with a simple TCRT5000 sensor to detect the black stripe on my energymeter at home. What i want is to detect the pulses and send them to thingspeak with an ESP8266.

All the components are working. TCRT5000 gives 50 on serial monitor when it detects nothing and 400 when the black stripe comes by, so this is enough to make a pulse out of it..

The esp8266 i got to communicate with my accesspoint and send data to thingspeak.

But now combining the two in one project is a real pain in the.....

Because of thingspeak i can only send data every 15 seconds. But in the meanwhile i have detected several pulses. So how am i going to fix this??

I have an TFT screen with shield and a SD card slot in it, so maybe i can buffer some data here, and then send a file from the SD every 15 seconds to thingspeak???

i want to get a nice graph with current energy consumption and a total counter. So i can someone help me or show me a good simple example sketch???

With kind regards, Bert.

Programming skills: Beginner and learning through the google machine and viewing other sketches and copy an pasting bits and pieces.

Count the pulses and send the updated count every minute or so. You can either reset the count each time you send (which makes the current consumption graph easy but the total harder) or let it run (which has the opposite problem). Google "arduino pulse counter sketch" for lots of examples.

If you post what code you already have (even if its not fully working) it will help others to help you.

Thank you. I will post my code tommorow night from work. In the meantime i woll search some examples.

So here is my code:
Sorry for the dutch comments.
How can i program it so that the pulses are still being counted while uploading the data???
Can't get it to work that way.

kwh_teller_meerdere_uploads_upload_file.ino (4.1 KB)

Putting the code for updating the internet in a separate function makes it easy to read but unfortunately its causing you problems with real time updates. You will find that programming to make the arduino do two things at once is a lot more difficult than just doing one.

There are two ways I can suggest.

  1. Re-wire your sensor so that you can electrically adjust the threshold (using a variable resistor for example) then switch to using digitalRead() instead. Change the connection to one of the pins thats set up for hardware interrupt (see Arduino Playground - Interrupts) then you can use an interrupt service routine to do the pulseCount++ step asynchronously with the rest of your program.

  2. Put all the code from updateFunction() back into the main loop(). Make sure your code never holds up waiting for serial input by testing for serial2.available() before doing serial2.find(). You will also need to have flags for what stage of the serial conversation you are up to because you cant wait for input from the ESP at any stage if you want to maintain realtime logging. You may well find its easiest to read the serial output of the ESP at the top (see Convert serial.read() into a usable string using Arduino - Stack Overflow) then use indexof() rather than serial2.find() to keep track of the status.

Thanks for the reply.
Part 1 i understand, but i have to resolder ans find out a way to make the threshold controllable. I'm thinking of a pot and see what happens.

Part 2 i don't understand.

Maybe i have to accept that i will lose some pulses, to make ik not to difficult for my programming skills.

thanks anyway

I have rewritten my code to work with an interrupt, and it works. Now i only have to make sure my sensor is reading my ferraris wheel good and the pulses will be detected. Don't know if my threshold is high enough to create a pulse.

Glad to see you took the easy way out :slight_smile: If you have a variable resistor on your sensor you should be able to adjust it to get the right level to reliably trigger your digital pin.

Sorry if my second option wasnt well explained- its a much more complicated way of doing it. (I wasnt brave enough to post code in case it didnt work..)

I think my kWh meter doesn't produce enough voltage difference to produce a 0 and 1 on the digital input.
How should I connect a variable resistor? Now I have 5v to input pin of led and emitter 100ohm from led to ground an a 10k ohm from collector to ground. Digital signal comes from collector pin to d2 on arduino.
Should I replace the 10k for a pot 0-20k to test?

Anyone a idea one this one? Or is my only option to amplify the outgoing signal?

Can you use a comparator, like the LM358 on the Uno? Can set the compared-to level to what ever level is needed to get a good pulse out of the comparator.

Don't havr any other parts available right now. I'm going to try with a potmeter first. Maybe that will work.

Is there someone who can tell me the difference in connecting the tcrt5000 according to the attachments?

esquema.jpg