Help with Arduino Project

I am currently tasked with creating my own datalogger out of an arduino . I am creating an urban rooftop turbine array, and have to measure the current and voltage outputs of 4 motors on a loop. I have a Voltage sensor and current sensor for each motor, and also need to the results to both save to a micro SD card, as well as be able to print and just be checked by Serial Monitor. I am having a lot of trouble with the code, as well as the wiring with the arduino. Any help is greatly appreciated, I am using an Arduino Mega 2560

Sebastian, welcome to the forum!

An interesting project to be sure, but perhaps you could help us by providing some details. For example:

  1. What are the current and voltage sensors you are using?
  2. Perhaps you could provide a diagram of how you have wired everything together?
  3. Could you also post the code you have written so far. Please make sure to enclose it using the CODE tags. This makes it much easier for us to read.
  • Jack Christensen has made a great Arduino data logger example here; see the schematic and complete sketch.
    Serial data is saved to a SD card as it is received.

  • Jack has a board offering he sells, however , you can use your own Arduino board.

1 Like

DroneBotWorkShop made a video with transcript and code about data logging ("record and playback") using an SD Card reader with Arduino Uno.

1 Like

Thanks for the reply, My apologies for checking back so late. I will get all those for you and a picture when I finish with classes for today. Thanks !

#include <SPI.h>
#include <SD.h>

const int numMotors = 4; // Number of motors in the array
const int currentPins[] = {A0, A1, A2, A3}; // Analog pins for current sensors
const int voltagePins[] = {A4, A5, A6, A7}; // Analog pins for voltage sensors
const int chipSelect = 10; // Pin for the micro SD card's chip select

File dataFile;

void setup() {
  // Initialize serial communication
  Serial.begin(9600);
  
  // Initialize SD card
  if (!SD.begin(chipSelect)) {
    Serial.println("SD card initialization failed.");
    return;
  }
  Serial.println("SD card initialized successfully.");

  // Create a new file on the SD card
  dataFile = SD.open("motor_data.txt", FILE_WRITE);
  if (!dataFile) {
    Serial.println("Error opening file.");
    return;
  }
  Serial.println("File opened successfully.");

  // Write header to the file
  dataFile.println("Motor\tCurrent (A)\tVoltage (V)");
  dataFile.flush();

  // Close the file
  dataFile.close();

  // Print header to serial monitor
  Serial.println("Motor\tCurrent (A)\tVoltage (V)");
}

void loop() {
  // Open the file
  dataFile = SD.open("motor_data.txt", FILE_WRITE);

  // Read sensor data and write to file
  for (int motor = 1; motor <= numMotors; motor++) {
    float current = readCurrent(motor);
    float voltage = readVoltage(motor);
    if (dataFile) {
      dataFile.print(motor);
      dataFile.print("\t");
      dataFile.print(current, 4); // Print current with 4 decimal places
      dataFile.print("\t\t");
      dataFile.println(voltage, 4); // Print voltage with 4 decimal places
      dataFile.flush(); // Flush buffer to ensure data is written to SD card
    }
    // Print data to serial monitor
    Serial.print(motor);
    Serial.print("\t");
    Serial.print(current, 4);
    Serial.print("\t\t");
    Serial.println(voltage, 4);
    delay(100); // Delay for stability
  }

  // Close the file
  dataFile.close();

  // Wait for next reading
  delay(1000); // Adjust as needed
}

float readCurrent(int motor) {
  // Placeholder function to read current from sensor
  // Replace with your actual code to read current
  return analogRead(currentPins[motor - 1]) * 5.0 / 1023.0; // Convert analog reading to current (assuming 5V reference voltage)
}

float readVoltage(int motor) {
  // Placeholder function to read voltage from sensor
  // Replace with your actual code to read voltage
  return analogRead(voltagePins[motor - 1]) * 5.0 / 1023.0; // Convert analog reading to voltage (assuming 5V reference voltage)
}

This is the code I have / 
![5C11EE07-99BC-4CCF-8B56-C945D1FF18C2|375x500](upload://NkMK4m0DnSDw2K4XCIvZduoyjA.jpeg)

Please let me know if you can acess the photos, this is how I currently have it wired. I am using an ACS712 Current Sensor , and a WWMZDiB Voltage Sensor

At a glance the code looks plausible. Can you describe the difficulties you are having, like what it does it shouldn't or does not do it should?

Teeny tip: get over not using zero as the first number of N things. In C/C++ ppl usually can write

    for (int motor = 0; motor < numMotors; motor++) {

which is practically idiomatic for doing something numMotors number of times, then you don't have to fiddle later to accommodate the fact that array indices begin at zero, viz:

return analogRead(voltagePins[motor]) * 5.0 / 1023.0;

If you are near a nice warm fire or under the umbrella at the beach, read this old timer's passionate prose:

https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.