Go Down

Topic: Problem trying to add several types of sensors to Arduino sketch (Read 172 times) previous topic - next topic

PadraigL

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!

Code: [Select]
// 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);
}

terryking228

Hi,

This is a very common problem: adding two or more working sketches into one sketch.

Putting a bunch of code together and then getting a Zillion error messages is No Fun!

FIRST! Start by understanding both sketches and the resources and libraries they use before attempting to stitch them together. If there is any part of either sketch you don't understand, that will be the part that causes you trouble.  Get each one working separately.

Here is a suggested approach: 
https://arduinoinfo.mywikis.net/wiki/CombiningArduinoSketches
Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

PadraigL

Hi Terry,

Thanks for your reply.


I may have bitten off more than I can chew with this, I have no past experience with setting up sensors on an Arduino. It took me ages to setup the DHTs to sd, yet this seems much more difficult.

I updated my sketch with some code taken from the yl-69 example. I get the error:
Code: [Select]
'readSoil' was not declared in this scope
From researching I believe this is due to not declaring the variable. But I cannot see anywhere this was declared in the original YL-69 sketch that I didn't copy over, yet that sketch works fine by itself.

Any idea on how I can fix that error?

Both the updated and example YL-69 sketch i used are below.

Updated Code:

Code: [Select]
// 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);

int val = 0; //value for storing moisture value
int soilPin = A0;//Declare a variable for the soil moisture sensor

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");
   Serial.print("Soil Moisture = ");
   Serial.println(readSoil());
   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();
   
  }
  int readSoil()
{

    val = analogRead(soilPin);//Read the SIG value form sensor
    return val;//send current moisture value
}

    delay(10000);
}



YL-69 code that some bits were copied over from:

Code: [Select]
/*  Soil Mositure Basic Example
    This sketch was written by SparkFun Electronics
    Joel Bartlett
    August 31, 2015

    Basic skecth to print out soil moisture values to the Serial Monitor

    Released under the MIT License(http://opensource.org/licenses/MIT)
*/

int val = 0; //value for storing moisture value
int soilPin = A0;//Declare a variable for the soil moisture sensor
int soilPower = 7;//Variable for Soil moisture Power

//Rather than powering the sensor through the 3.3V or 5V pins,
//we'll use a digital pin to power the sensor. This will
//prevent corrosion of the sensor as it sits in the soil.

void setup()
{
  Serial.begin(9600);   // open serial over USB

  pinMode(soilPower, OUTPUT);//Set D7 as an OUTPUT
  digitalWrite(soilPower, LOW);//Set to LOW so no power is flowing through the sensor
}

void loop()
{
Serial.print("Soil Moisture = ");   
//get soil moisture value from the function below and print it
Serial.println(readSoil());

//This 1 second timefrme is used so you can test the sensor and see it change in real-time.
//For in-plant applications, you will want to take readings much less frequently.
delay(1000);//take a reading every second
}
//This is a function used to get the soil moisture content
int readSoil()
{

    digitalWrite(soilPower, HIGH);//turn D7 "On"
    delay(10);//wait 10 milliseconds
    val = analogRead(soilPin);//Read the SIG value form sensor
    digitalWrite(soilPower, LOW);//turn D7 "Off"
    return val;//send current moisture value
}

pert

You are defining your readSoil function inside the loop function. You can't do that.

A very helpful troubleshooting tool is the Auto Format feature (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor). If you do an Auto Format and then compare the resulting indentation to your intended program structure, it will quickly point you to where there is a missing or extra brace.

Another useful feature of the Arduino IDE/Arduino Web Editor is that when you place the cursor next to one bracket, it puts a box around the matching bracket. In the Arduino IDE, if the cursor is next to the closing bracket and the opening bracket is off the screen then it will show the opening bracket line in a tool tip after a short delay.

PadraigL

I corrected the code using the auto-format you mentioned. I also changed bits around and I've since got my YL-69 soil moisture sensor working along with my 4 DHT11s.
My only task now is the add the other YL-69s to the sketch, then I'm all set.

Thanks for the answers folks, appreciate it.

I'll leave the sketch i'm currently using in case anyone comes across this with a similiar aim.

Code: [Select]
// 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);

int sensor_pin = A0;
int output_value ;

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) {
   output_value= analogRead(sensor_pin);
   output_value = map(output_value,1024,0,0,100);
   Serial.print("Moisture : ");
   Serial.print(output_value);
   Serial.println("%");
   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.print(",");
   dataFile.print("Moisture : ");
   dataFile.print(",");
   dataFile.print(output_value);
   dataFile.close();
   
  }
  // if the file didn't open, print an error:
  else
    Serial.println("error opening DHT11Log.txt");
    delay(10000);
}

Go Up