[solved] DFrobot SEN0501 arduino code

i have purchased DFrobot SEN0501 ,i downloaded the needed libraries but the code is not compiling using arduino 1.8.13 on windows 10

Here is the code that i have used

#include "DFRobot_EnvironmentalSensor.h"
#if defined(ARDUINO_AVR_UNO)||defined(ESP8266)
#include <SoftwareSerial.h>
#endif

#define MODESWITCH        /*UART:*/1 /*I2C: 0*/

#define    SEN0501_DEFAULT_DEVICE_ADDRESS             0x22

#if MODESWITCH
#if defined(ARDUINO_AVR_UNO)||defined(ESP8266)
  SoftwareSerial mySerial(/*rx =*/4, /*tx =*/5);
  DFRobot_EnvironmentalSensor environment(/*addr =*/SEN0501_DEFAULT_DEVICE_ADDRESS, /*s =*/&mySerial);
#else
  DFRobot_EnvironmentalSensor environment(/*addr =*/SEN0501_DEFAULT_DEVICE_ADDRESS, /*s =*/&Serial1);
#endif
#else
DFRobot_EnvironmentalSensor environment(/*addr = */SEN0501_DEFAULT_DEVICE_ADDRESS, /*pWire = */&Wire);
#endif
void setup()
{
#if MODESWITCH
  //Init MCU communication serial port
#if defined(ARDUINO_AVR_UNO)||defined(ESP8266)
  mySerial.begin(9600);
#elif defined(ESP32)
  Serial1.begin(9600, SERIAL_8N1, /*rx =*/D3, /*tx =*/D2);
#else
  Serial1.begin(9600);
#endif
#endif
  Serial.begin(115200);
  while(environment.begin() != 0){
    Serial.println(" Sensor initialize failed!!");
    delay(1000);
  }
  Serial.println(" Sensor  initialize success!!");
}

void loop()
{
  //Print the data obtained from sensor
  Serial.println("-------------------------------");
  Serial.print("Temp: ");
  Serial.print(environment.getTemperature(TEMP_C));
  Serial.println(" ℃");
  Serial.print("Temp: ");
  Serial.print(environment.getTemperature(TEMP_F));
  Serial.println(" ℉");
  Serial.print("Humidity: ");
  Serial.print(environment.getHumidity());
  Serial.println(" %");
  Serial.print("Ultraviolet intensity: ");
  Serial.print(environment.getUltravioletIntensity());
  Serial.println(" mw/cm2");
  Serial.print("LuminousIntensity: ");
  Serial.print(environment.getLuminousIntensity());
  Serial.println(" lx");
  Serial.print("Atmospheric pressure: ");
  Serial.print(environment.getAtmospherePressure(HPA));
  Serial.println(" hpa");
  Serial.print("Altitude: ");
  Serial.print(environment.getElevation());
  Serial.println(" m");
  Serial.println("-------------------------------");
  delay(500);
}

i got these errors

Arduino: 1.6.5 (Windows 8.1), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

Build options changed, rebuilding all

C:\Users\OneDrive\Pictures\arduino-1.6.5-r5\libraries\DFRobot_EnvironmentalSensor\src\Wire.cpp: In member function 'uint8_t TwoWire::requestFrom(uint8_t, uint8_t, uint8_t)':
C:\Users\OneDrive\Pictures\arduino-1.6.5-r5\libraries\DFRobot_EnvironmentalSensor\src\Wire.cpp:85:68: error: too many arguments to function 'uint8_t twi_readFrom(uint8_t, uint8_t*, uint8_t)'

  • uint8_t read = twi_readFrom(address, rxBuffer, quantity, sendStop);
    ^
    In file included from C:\Users\amr42\OneDrive\Pictures\arduino-1.6.5-r5\libraries\DFRobot_EnvironmentalSensor\src\Wire.cpp:26:0:
    C:\Users\OneDrive\Pictures\arduino-1.6.5-r5\libraries\DFRobot_EnvironmentalSensor\src\twi.h:43:11: note: declared here
    uint8_t twi_readFrom(uint8_t, uint8_t*, uint8_t);
    ^
    C:\Users\OneDrive\Pictures\arduino-1.6.5-r5\libraries\DFRobot_EnvironmentalSensor\src\Wire.cpp: In member function 'uint8_t TwoWire::endTransmission(uint8_t)':
    C:\Users\OneDrive\Pictures\arduino-1.6.5-r5\libraries\DFRobot_EnvironmentalSensor\src\Wire.cpp:140:76: error: too many arguments to function 'uint8_t twi_writeTo(uint8_t, uint8_t*, uint8_t, uint8_t)'
    int8_t ret = twi_writeTo(txAddress, txBuffer, txBufferLength, 1, sendStop);
    ^
    In file included from C:\Users\OneDrive\Pictures\arduino-1.6.5-r5\libraries\DFRobot_EnvironmentalSensor\src\Wire.cpp:26:0:
    C:\Users\OneDrive\Pictures\arduino-1.6.5-r5\libraries\DFRobot_EnvironmentalSensor\src\twi.h:44:11: note: declared here
    uint8_t twi_writeTo(uint8_t, uint8_t*, uint8_t, uint8_t);
    ^
    Error compiling.

any help how to solve this ?
libraries.zip (48.4 KB)

The errors are saying twi_readFrom() received too many arguments. I found twi.c (dot c, not dot h) with twi_readFrom(), and it has four arguments... but could not trace it back to DFRobot...h or softwareserial.h

Hello @xfpd

thank you very much for your reply

i believe it is a problem of an old code that needs to be adapted to new arduino standards but unfortunately no idea how

i even opened an issue on the manufacturer's github but they did not reply

that is why i am seeking help from other friends here that may faced a similar problem or so during their work

It compiles just fine for me using Arduino 1.8.19.

Maybe it is time to update your environment?

Hello @blh64

let me give it a try and will give u my feed back

the code compiled using IDE 1.8.19

attached are the used libraries

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