Hi,
I am only beginning to learn how to use Arduino, and am having trouble with a sketch.
I'm trying to send multiple sensor data (DHT11, BH1750, YL-69) to a micro sd. Currently I can send four DHT11 sensors data to the sd successfully. The sketch I'm using is further in this post.
However, I don't know how to add the multiple BH1750 and YL-69 sensors to the sketch to also measure and write their data to the micro-sd.
I have tried copying some of the code from the example sketches and other projects people have used, but trying to combine them with my existing sketch just throws up many errors. Could anyone suggest additions to my sketch to allow me to write the data from multiple BH1750 and YL-69 sensors to my micro-sd?
Below is the current sketch I used for writing to sd the 4xDHT11 sensors.
Thanks for reading this!
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain https://github.com/adafruit/DHT-sensor-library
#include "DHT.h"
#include <SPI.h> // Include SPI library (needed for the SD card)
#include <SD.h> // Include SD library
File dataFile;
#define DHTPIN0 10 // what pin we're connected to
#define DHTPIN1 11 // what pin we're connected to
#define DHTPIN2 12 // what pin we're connected to
#define DHTPIN3 13 // what pin we're connected to
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// How to connect sensors
// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
DHT dht0(DHTPIN0, DHTTYPE);
DHT dht1(DHTPIN1, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);
DHT dht3(DHTPIN3, DHTTYPE);
unsigned long time;
void setup() {
Serial.begin(9600);
while (!Serial)
; // wait for serial port to connect. Needed for native USB port only
Serial.print("Initializing SD card...");
if (!SD.begin()) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
Serial.println("DHTxx test!");
dht0.begin();
dht1.begin();
dht2.begin();
dht3.begin();
delay(2000);
}
uint16_t line = 1;
void loop() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h0 = dht0.readHumidity();
float h1 = dht1.readHumidity();
float h2 = dht2.readHumidity();
float h3 = dht3.readHumidity();
float t0 = dht0.readTemperature();
float t1 = dht1.readTemperature();
float t2 = dht2.readTemperature();
float t3 = dht3.readTemperature();
dataFile = SD.open("DHT11Log.csv", FILE_WRITE);
// if any result below is NaN (not a number) then something went wrong!
if (dataFile) {
Serial.print("Humidity : sensor 0, ");
Serial.println(h0);
Serial.print(" sensor 1, ");
Serial.println(h1);
Serial.print(" sensor 2, ");
Serial.println(h2);
Serial.print(" sensor 3, ");
Serial.println(h3);
Serial.println(" %\t");
Serial.print("temperature : sensor 0, ");
Serial.println(t0);
Serial.print(" sensor 1, ");
Serial.println(t1);
Serial.print(" sensor 2, ");
Serial.println(t2);
Serial.print(" sensor 3, ");
Serial.println(t3);
Serial.println(" %\t");
dataFile.print(line++);
dataFile.print(": Temperature = ");
dataFile.print(",");
dataFile.print(t0);
dataFile.print(",");
dataFile.print(t1);
dataFile.print(",");
dataFile.print(t2);
dataFile.print(",");
dataFile.print(t3);
dataFile.print(",");
dataFile.print("°C, Humidity = ");
dataFile.print(",");
dataFile.print(h0);
dataFile.print(",");
dataFile.print(h1);
dataFile.print(",");
dataFile.print(h2);
dataFile.print(",");
dataFile.print(h3);
dataFile.print(",");
dataFile.println("%");
dataFile.close();
}
// if the file didn't open, print an error:
else
Serial.println("error opening DHT11Log.txt");
delay(10000);
}