Hi guys,
i've just received AM2315 sensor from adafruit http://www.adafruit.com/products/1293
All works well with Arduino, I've used an example code that you find here: https://github.com/adafruit/Adafruit_AM2315/blob/master/examples/am2315test/am2315.pde
Now I'm trying to make an easy porting on Maple so I have modified the libraries to make compile with maple. Everything seems ok, no compile error but I receive always zero from data.
For the porting from Arduino I've just changed only:
1) Wire.begin() in Wire.begin(uint8 pin_SDA, uint8 pinSCL)
2) Wire.write() with Wire.send()
3) Wire.read() with Wire.receive()
4) I have keep 10K pullup resistors on SDA and SCL lines (or I have to change in 5Kohm ?)
The Pinout is this:
// Connect RED of the AM2315 sensor to 5.0V (I use 'Vin' pin because I'm loading my sketch from USB)
// Connect BLACK to Ground
// Connect WHITE to i2c clock - on pin 4 with a pullup of 10K
// Connect YELLOW to i2c data - on pin 5 with a pullup of 10K
the code of AM2315 library is:
Adafruit_AM2315.h:
#if (ARDUINO >= 100)
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "Wire.h"
#define AM2315_I2CADDR 0x5C
#define AM2315_READREG 0x03
class Adafruit_AM2315 {
public:
Adafruit_AM2315();
boolean begin(uint8 pinSDA, uint8 pinSCL); // modified for Maple
float readTemperature(void);
float readHumidity(void);
private:
boolean readData(void);
float humidity, temp;
};
Adafruit_AM2315.cpp:
#include <stdint.h> //add for Maple
#include "Adafruit_AM2315.h"
Adafruit_AM2315::Adafruit_AM2315() {
}
boolean Adafruit_AM2315::begin(uint8 pinSDA, uint8 pinSCL) { //function modified for Maple
Wire.begin(pinSDA, pinSCL);
// try to read data, as a test
return readData();
}
boolean Adafruit_AM2315::readData(void) {
uint8_t reply[10];
SerialUSB.println("read data");
Wire.beginTransmission(AM2315_I2CADDR);
Wire.send(AM2315_READREG); //function modified for Maple
Wire.send(0x00); // start at address 0x0 //function modified for Maple
Wire.send(4); // request 4 bytes data //function modified for Maple
Wire.endTransmission();
// for reasons unknown we have to send the data twice :/
// whats the bug here?
Wire.beginTransmission(AM2315_I2CADDR);
Wire.send(AM2315_READREG); //function modified for Maple
Wire.send(0x00); // start at address 0x0 //function modified for Maple
Wire.send(4); // request 4 bytes data //function modified for Maple
Wire.endTransmission();
Wire.requestFrom(AM2315_I2CADDR, 8);
while(!Wire.available());
for (uint8_t i=0; i<8; i++) {
reply[i] = Wire.receive(); //function modified for Maple
SerialUSB.println(reply[i], HEX);
}
if (reply[0] != AM2315_READREG) return false;
if (reply[1] != 4) return false; // bytes req'd
humidity = reply[2];
humidity *= 256;
humidity += reply[3];
humidity /= 10;
//Serial.print("H"); Serial.println(humidity);
temp = reply[4];
temp *= 256;
temp += reply[5];
temp /= 10;
//Serial.print("T"); Serial.println(temp);
return true;
}
float Adafruit_AM2315::readTemperature(void) {
if (! readData()) return NAN;
return temp;
}
float Adafruit_AM2315::readHumidity(void) {
if (! readData()) return NAN;
return humidity;
}
Maple code is:
#include <Wire.h>
#include <Adafruit_AM2315.h>
Adafruit_AM2315 am2315;
void setup() {
delay(5000);
// SerialUSB.begin(9600);
SerialUSB.println("AM2315 Test!");
if (! am2315.begin(4,5)) {
SerialUSB.println("Sensor not found, check wiring & pullups!");
while (1);
}
}
void loop() {
SerialUSB.print("Hum: "); SerialUSB.println(am2315.readHumidity());
SerialUSB.print("Temp: "); SerialUSB.println(am2315.readTemperature());
delay(1000);
}
I always receive 0 from Wire.receive() function. Any suggestion is appreciated!
(Edit: I changed markup to try to make it easier to read, hope this helps. gbulmer)