(Assistance Requested) AC Voltage Sketch Assistance

Are there any folks on here willing to help with a sketh i am trying to get to work?

What I am using :
Arduino Uno, RTC w/Data Logging Shield, AC Voltage Sensor

What I am wishing to do:
I would like to measure 120VAC Voltage, and log it to the SD Card along with a Time Stamp from the RTC

What I am struggling with:
I have the AC Voltage Sensing working and was able to print the AC Voltage on the Serial monitor. I was then able to get it to log to the SD Card but when I went to add the RTC timestampingto it i managed to mess the sketch all up and dont know what i did wrong.

I am a hack at this and am not schooled in the ways of elegant sketch writing i cut and paste and hope, well that didnt work in this case because I am now at a point where I have goofed my sketch up and didnt save a virgin copy along the way (lesson learned).

Can anyone look at the included sketch and offer guidance on what im doing wrong? I have included the sketch as I have it but could really use some assistance

Thanks to whomever responds to my call for HELP!


AC_Voltage_Logger_Test_3INWORK.ino (3.83 KB)

is these lines supposed to be outside the loop function?

File  datafile = SD.open("datalog.txt", FILE_WRITE);
  // if the file is available, write to it:
  if (datafile) {




I have it working ALL except logging the info to the card. I have the data printing to the Serial Monitor now but CANNOT get the data sent to the SD Card. the SD Card Initialzes but thats where it stops

I now need to get the data to go to the SD Card

Volt_Test.ino (2.41 KB)

Can you please put your code inline with </> code tags.
Many of us can’t see it on mobile devices.

// EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3

#include <Wire.h>
#include "RTClib.h"
#include <SPI.h>
#include <SD.h>
#include "EmonLib.h"             // Include Emon Library
EnergyMonitor emon1;             // Create an instance

RTC_DS1307 rtc; // define the Real Time Clock object

const int chipSelect = 9;

int i = 0;
float tenvals = 0.0;
float minval = 1000;
float maxval = 0.0;

void setup() {  
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only

if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);

      while (!Serial) {
      // wait for serial port to connect. Needed for native USB port only
  Serial.print("Initializing SD card...");
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1); }

{  Serial.println("card initialized.");
 emon1.voltage(A0, 579, 1.7);  // Voltage: input pin, calibration, phase_shift


void loop() {

 String dataString = "";
  emon1.calcVI(20,2000);         // Calculate all. No.of half wavelengths (crossings), time-out

 float Vrms = (emon1.Vrms - 3);
 if (Vrms < 0) { Vrms = 0.0; }
 tenvals += Vrms;
 if (minval > Vrms) { minval = Vrms; }
 if (maxval < Vrms) { maxval = Vrms; }

 if (i == 10)
  Serial.print("Avg: ");
  Serial.print(" Vrms: ");
  Serial.print("  ");

  i = 0;
  tenvals = 0.0;
  minval = 1000.0;
  maxval = 0.0;

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));

void loop (); 

    DateTime now = rtc.now();
    Serial.print(now.month(), DEC);
    Serial.print(now.day(), DEC);
    Serial.print(now.year(), DEC);
    Serial.print("  ");
    Serial.print("  ");
    Serial.print(now.hour(), DEC);
    Serial.print(now.minute(), DEC);
    Serial.print(now.second(), DEC);

First thing i see is the use of String (Capital S).
In a small simple project, not the best, but we can look away!
The real sin, is recreating the String every pass around loop().
Create the String once as a global up the top... perhaps preallocate the size to reduce memory fragmentation.(The good news is that you don’t use datastring in the code..!)

You don’t need to wait for Serial to initialise twice in setup()... once is enough.
You could also declare ‘now’ globally, and only update the value within loop()

Use Ctrl-T in the IDE to reformat your code and correct the indentation, it will be easier to follow.

Finally, in your most recent code, you don’t have any lines that try to write to the SD card....?

Apart from the SD card, does that code do what you’re expecting?
It looks like it’s spitting out data every 1 sec.
It would be a good idea to change the delay() to use millis() timing, and depending on your requirement, only write to SD when necessary, as there’s no point wearing down the SD memory with unnecessary writes.

Ok, back to you.

I would like to thank everyone for the assistance I have a working sketch now. If anyone would like any of the information just ask Ill be happy to share.

What I am using :
Arduino Uno, RTC w/Data Logging Shield, AC Voltage Sensor

What I am doing:
Im measuring one phase of 120VAC using a ZMPT101B (AC Volt Sensor) and logging it to the SD Card along with a Time Stamp from the RTC…

Happy Camper!