Voltage and Current monitoring.

Hey there!

I've just started with this kind of programming.
Now at my internship I need to setup a programme to monitor the Voltage and Current from some power supplies.
This is what I thought was a good setup for not having that much experiance with C programming:

void setup() {
Serial.begin(9600);
int pin1 = A2;
int sensorlValue = analogRead(A0);
}

void loop() {
if (int pin1 = HIGH);
{
Serial.print("Time : int pin1 ");
}
{
Serial.print("Voltage: ");
Serial.print(float voltage = (sensorValue*(5.00/1023)));
Serial.print(
}
{
Serial.print("Current: ");
Serial.print(float current = (((sensorValue*(5.0/1023))/15)));
Serial.print(
delay(1000);
}
{
File.close(Libraries/Documents/Arduino);
}

}

I have to write the data to an SD card output. I tried to convert that function to a hard drive but that didn't work. Could someone help me further with this please?

Beforehand, thank you!

You have a lot of braces {} that do nothing because they don't have any associated conditional statement or function. Thus, among other things, you are closing a file 1000 times a second.

There is so much wrong with your program that it is difficult to know where to begin

 int pin1 = A2;
  int sensorlValue = analogRead(A0);

These 2 variables are declared in setup() so will only be available in that function. Even if it did work you are only reading the value of A0 once, not repeatedly.

 if (int pin1 = HIGH);

This is assigning the value of HIGH to the newly declared variable pin1. If you had used == instead of = then it would be checking whether the random value assigned to the newly declared pin1 variable is HIGH. In any case, the trailing semicolon is the only code executed if the test return true. You need to do a digitalRead() of pin1 and test that value.

   Serial.print(
      delay(1000);
  }

I have no idea what you are trying to do here.

As I said, I'm really sorry if you can't make up anything out of my code.
This is what I tought would work since I have nearly no further experiance with this kind of programming.
I only know the really small basics.
But thank you anyway for feedback.
All I'm trying to do is learn.

How can you measure both the voltage and current from one analog reading?

I've got the datasheet of the analog connector. So all I had to do was create a different formula for the calculation of the current. The resistance is indicated on the sheet from Ref. 5.1 V and 15 Ohm.

Atleast that's how I think that it works on Arduino.

Oh, so it's for some specific device that is permanently connected to the power supply. I see.

That is indeed the case. For Other units all I would have to do is change the values within the fomula.

My real problem remains that I know how to get the values and the data manually, but it has to be automated with arduino. And I don't really know how to code it.

How to use this forum

Please use code tags.

Read this before posting a programming question


  File.close(Libraries/Documents/Arduino);

OK, get a C programming tutorial and start reading it.

#include <SD.h>

int cs_pin = 10;
int pow_pin = 8;

float refresh_rate = 0.0;

void setup() {
  Serial.begin(9600);
  Serial.println("Initializing Card");
  pinMode(CS_pin, OUTPUT);

  pinMode(pow_pin, OUTPUT);
  digitalWrite(pow_pin, HIGH);

  if(!SD.begin(CS_pin))
  {
    Serial.println("Card Failed!");
    return;
  }
  Serial.println("Card Ready.")
  
  File dataFile = SD.open("Voltage & Current", FILE_WRITE);
  if(dataFile)
  {
    dataFile.println(dataString);
    dataFile.close();
    Serial.println(dataString);
  }
  else
  {
    Serial.println("Couldn't create datalog");
  }
  File commandFile = SD.open("COMMANDS.txt");
  if (commandFile)
  {
    Serial.println("Reading Command File")

    float decade = pow(10, (cammandFile.available() - 1));
    while(commandFile.available())
    {
      float temp = cammandFile.read() - '0');
      refresh_rate = temp*decade+refresh_rate;
      decade = decade/1-;
    }
    Serial.print("Refresh Rate = ");
    Serial.print(refresh_rate);
    Serial.prinln("ms");
  }
 
   else
  {
    Serial.println("Couldn't read command file.");
    return;
  }
}

void loop() {
  
 String dataString = 

 File dataFile = SD.open("Voltage & Current.txt", FILE_WRITE);
  if(dataFile)
  {
    dataFile.println(dataString);
    dataFile.close();
    Serial.println(dataString);
  }
  else
  {
    Serial.println("Couldn't create datalog");
  }

  delay (refresh_rate);
}

This is how far I’ve gotten now. If I’m right the only thing remaining should be the formula for the actual Voltage and Current data

Does it compile when you try it ?

  String dataString =

looks like a very unlikely line of valid code

I know that was a piece i didn’t finish yet. But I’ll start testing everything tomorrow.

delay() takes an integer parameter, not a float.

#include <SD.h>

int cs_pin = 10;
int pow_pin = 8;

float refresh_rate = 0.0;
int val = analogRead(A0);
long id = 1;

void setup() {
  Serial.begin(9600);
  Serial.println("Initializing Card");
  pinMode(CS_pin, OUTPUT);

  pinMode(pow_pin, OUTPUT);
  digitalWrite(pow_pin, HIGH);

  if(!SD.begin(CS_pin))
  {
    Serial.println("Card Failed!");
    return;
  }
  Serial.println("Card Ready.")
  
  File dataFile = SD.open("Voltage & Current", FILE_WRITE);
  if(dataFile)
  {
    dataFile.println(dataString);
    dataFile.close();
    Serial.println(dataString);
  }
  else
  {
    Serial.println("Couldn't create datalog");
  }
  File commandFile = SD.open("COMMANDS.txt");
  if (commandFile)
  {
    Serial.println("Reading Command File")

    float decade = pow(10, (cammandFile.available() - 1));
    while(commandFile.available())
    {
      float temp = cammandFile.read() - '0');
      refresh_rate = temp*decade+refresh_rate;
      decade = decade/10;
    }
    Serial.print("Refresh Rate = ");
    Serial.print(refresh_rate);
    Serial.prinln("ms");
  }
 
  else
  {
    Serial.println("Couldn't read command file.");
    return;
  }

  File logFile = SD.open("Voltage & Current.csv", FILE_WRITE);
  if (logFile)
  {
    logFile.println)", ,");
    String header = "Voltage, Current";
    logFile.println(header);
    logFile.close();
    Serial.println(header);
  }
  else
  {
    Serial.println("Couldn't open log file");
  }
}

void loop() {
  int Voltage = (val*(5/1024));
  int Current = ((val*(5/1024))/15);

 String dataString = String(id) + ", " + String(Voltage) + ", " + String(Current);

 File dataFile = SD.open("Voltage & Current.csv", FILE_WRITE);
  if(dataFile)
  {
    dataFile.println(dataString);
    dataFile.close();
    Serial.println(dataString);
  }
  else
  {
    Serial.println("Couldn't create datalog");
  }
  id++;

  delay (refresh_rate);
}

According to the tutorial by Jeremy Blum it should work like that :confused:

Dr4g0n3:

#include <SD.h>

int cs_pin = 10;
int pow_pin = 8;

float refresh_rate = 0.0;
int val = analogRead(A0);
long id = 1;

void setup() {
  Serial.begin(9600);
  Serial.println(“Initializing Card”);
  pinMode(CS_pin, OUTPUT);

pinMode(pow_pin, OUTPUT);
  digitalWrite(pow_pin, HIGH);

if(!SD.begin(CS_pin))
  {
    Serial.println(“Card Failed!”);
    return;
  }
  Serial.println(“Card Ready.”)
 
  File dataFile = SD.open(“Voltage & Current”, FILE_WRITE);
  if(dataFile)
  {
    dataFile.println(dataString);
    dataFile.close();
    Serial.println(dataString);
  }
  else
  {
    Serial.println(“Couldn’t create datalog”);
  }
  File commandFile = SD.open(“COMMANDS.txt”);
  if (commandFile)
  {
    Serial.println(“Reading Command File”)

float decade = pow(10, (cammandFile.available() - 1));
    while(commandFile.available())
    {
      float temp = cammandFile.read() - ‘0’);
      refresh_rate = temp*decade+refresh_rate;
      decade = decade/10;
    }
    Serial.print("Refresh Rate = ");
    Serial.print(refresh_rate);
    Serial.prinln(“ms”);
  }

else
  {
    Serial.println(“Couldn’t read command file.”);
    return;
  }

File logFile = SD.open(“Voltage & Current.csv”, FILE_WRITE);
  if (logFile)
  {
    logFile.println)", ,");
    String header = “Voltage, Current”;
    logFile.println(header);
    logFile.close();
    Serial.println(header);
  }
  else
  {
    Serial.println(“Couldn’t open log file”);
  }
}

void loop() {
  int Voltage = (val*(5/1024));
  int Current = ((val*(5/1024))/15);

String dataString = String(id) + ", " + String(Voltage) + ", " + String(Current);

File dataFile = SD.open(“Voltage & Current.csv”, FILE_WRITE);
  if(dataFile)
  {
    dataFile.println(dataString);
    dataFile.close();
    Serial.println(dataString);
  }
  else
  {
    Serial.println(“Couldn’t create datalog”);
  }
  id++;

delay (refresh_rate);
}




According to the tutorial by Jeremy Blum it should work like that :confused:

Was that in reply to #11, or #13? Anyway, you should take tutorials with a large grain of salt. It’s amazing how wrong most of them are.

that was 11. And indeed i should not take them for granted. And it is a video from 2011 so there have been quite some patches since for sure!
I know you guys are right but I'll test this version first.

My bad! I meant comment #13