searching for a sample code
I'm searching for a sample code for Arduino microcontroller that read from 4 temperature sensors (KIT0021), 1 ultrasonic distance sensor (SEN0311), 1 turbidity sensor (SEN0189), 1 Analog Current Sensor (SEN0098-V2), 1 dc voltage sensor and all that is store hourly on sd card via sd card module….
Could any one help me in that???
You can try this sample code:
#include <OneWire.h>
#include <DallasTemperature.h>
#include <NewPing.h>
#include <SD.h>
#include <SPI.h>
// Pin definitions
#define ONE_WIRE_BUS 2 // Data wire for temperature sensors
#define TRIG_PIN 3 // Trigger pin for ultrasonic sensor
#define ECHO_PIN 4 // Echo pin for ultrasonic sensor
#define TURBIDITY_PIN A0 // Analog pin for turbidity sensor
#define CURRENT_SENSOR_PIN A1 // Analog pin for current sensor
#define VOLTAGE_SENSOR_PIN A2 // Analog pin for voltage sensor
#define SD_CS_PIN 10 // Chip select pin for SD card module
// Constants
#define MAX_DISTANCE 200 // Maximum distance for ultrasonic sensor
// Objects
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature tempSensors(&oneWire);
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
File dataFile;
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize temperature sensors
tempSensors.begin();
// Initialize SD card
if (!SD.begin(SD_CS_PIN)) {
Serial.println("SD card initialization failed!");
return;
}
Serial.println("SD card initialized.");
// Create or open the data file
dataFile = SD.open("data.txt", FILE_WRITE);
if (dataFile) {
dataFile.println("Timestamp,Temp1,Temp2,Temp3,Temp4,Distance,Turbidity,Current,Voltage");
dataFile.close();
} else {
Serial.println("Error opening data file!");
}
}
void loop() {
// Get timestamp
unsigned long timestamp = millis();
// Read temperature sensors
tempSensors.requestTemperatures();
float temp1 = tempSensors.getTempCByIndex(0);
float temp2 = tempSensors.getTempCByIndex(1);
float temp3 = tempSensors.getTempCByIndex(2);
float temp4 = tempSensors.getTempCByIndex(3);
// Read ultrasonic sensor
unsigned int distance = sonar.ping_cm();
// Read turbidity sensor
int turbidity = analogRead(TURBIDITY_PIN);
// Read current sensor
int current = analogRead(CURRENT_SENSOR_PIN);
// Read voltage sensor
int voltage = analogRead(VOLTAGE_SENSOR_PIN);
// Log data
logData(timestamp, temp1, temp2, temp3, temp4, distance, turbidity, current, voltage);
// Wait for one hour (3600000 milliseconds)
delay(3600000);
}
void logData(unsigned long timestamp, float temp1, float temp2, float temp3, float temp4, unsigned int distance, int turbidity, int current, int voltage) {
// Open the file for writing
dataFile = SD.open("data.txt", FILE_WRITE);
if (dataFile) {
// Write the data to the file
dataFile.print(timestamp);
dataFile.print(",");
dataFile.print(temp1);
dataFile.print(",");
dataFile.print(temp2);
dataFile.print(",");
dataFile.print(temp3);
dataFile.print(",");
dataFile.print(temp4);
dataFile.print(",");
dataFile.print(distance);
dataFile.print(",");
dataFile.print(turbidity);
dataFile.print(",");
dataFile.print(current);
dataFile.print(",");
dataFile.println(voltage);
dataFile.close(); // Close the file
Serial.println("Data logged.");
} else {
Serial.println("Error opening data file!");
}
}