简体   繁体   中英

Arduino reading several onewire sensors

I am using an Arduino that has several (3) sensors connected to it on digital pin 2 using normal mode (not parasite).

Two of the sensors is temperature sensors using the libraries "OneWire" ( Library Page ) and "DallasTemperature" ( Library on GitHub ). With the DallasTemperature-library it is easy to access the sensor values with the command "getTempCByIndex(int)".

My third sensor is a combined temperature- and humiditysensor. The provided code for this sensor was a separate library "DHT11". That library is not that good and I have a hard time trying to read sensor values with both DHT11- and DallasTemperature-library.

I think that the OneWire-library should be universal for all OneWire-devices and that the DallasTemperature-library is a wrapper for that library providing a good interface for some sensors.

Is there someone who can help me understand how to include the DHT11-library in the DallasTemperature-library? A good function would be "getDHT11HumidityByIndex(int)".

Or is it easier to write a new wrapper using OneWire? In that case, how would that work?

Now I just try to use the provided libraries with the code below. The program often fails to read the humidity sensor, gets the status (-2) : "Read sensor: Time out error" and the index of the sensors change during runtime. Is there some small change I can do to fix this?

#include <OneWire.h>
#include <DallasTemperature.h>
#include <dht11.h>  //Library for the humidity sensor.

// Data wire is plugged into port 2 on the Arduino.
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs).
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

// Declare object for Humidity sensor.
dht11 DHT11;



void setup(void)
{
  // Start serial port.
  Serial.begin(9600);

  // Start up the library.
  sensors.begin();
}

void loop(void)
{ 
  // Call sensors.requestTemperatures() to issue a global temperature 
  // request to all devices on the bus.
  sensors.requestTemperatures(); // Send the command to get temperatures.

  Serial.print("BEGIN-0#");
  Serial.print(sensors.getTempCByIndex(0));
  // You can have more than one IC on the same bus.
  // 0 refers to the first IC on the wire.
  Serial.println("#COMMIT");

  Serial.print("BEGIN-1#");
  Serial.print(sensors.getTempCByIndex(1));
  // You can have more than one IC on the same bus.
  // 0 refers to the first IC on the wire.
  Serial.println("#COMMIT");



  int chk = DHT11.read(ONE_WIRE_BUS);

  Serial.print("Read sensor: ");
  switch (chk)
  {
    case 0: Serial.println("OK"); break;
    case -1: Serial.println("Checksum error"); break;
    case -2: Serial.println("Time out error"); break;
    default: Serial.println("Unknown error"); break;
  }

  Serial.print("Humidity (%): ");
  Serial.println((float)DHT11.humidity, 2);

  Serial.print("Temperature (oC): ");
  Serial.println((float)DHT11.temperature, 2);
}

It would be reasonable to expect that two libraries accessing the same bus might cause a glitch when you transition from one to the other controlling the bus. But the 1 wire protocol is just one bit, and the default state is always 1. I would guess a small chance that there is a glitch when the DHTT11.read() starts. I understand the concept of passing the hardware layer interface object to the particular sensor class, but in this case the interface is one single pin. I would not start by rewriting the library.

The good advice would be to get a scope and logic analyzer on the bus. You need the scope to see that your wiring is not producing ringing or voltage sags. You need the logic analyzer because counting bits is mind numbingly tedious. This is a great excuse/justification to buy some instruments if so inclined. But we aren't doing this for a living so what can you do?

Decompose the problem:

  • Run 1 temp/humidity sensor on the bus with only the DHT11 library. Does it work? If yes, the library is stable, and your wiring is good. If no, stop and fix like this.

  • Add some big delay. These devices are bus powered and need time to charge up. There are surely guides on the data sheets. Or just add 2 seconds between reads. Does it work now? If yes, can push on delay and see what happens.

  • Now add the second temp only sensor. Does it stop working?

  • If it stops working, again add some big delay between the reads.

  • If still broken, switch to running just the temp sensor (you say this already works). Make the temp only sensor program work.

  • Add 3rd, 4th etc devices. If the function gets poor with more devices, start focusing on the hardware link layer. Again a scope is best route to insight if the signal is getting to the devices. The wire does matter with twisted pair giving you better distance. How you physically wire the devices will effect reflections that cause the signals to degraded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM