#define DHT11_PIN 1 // define analog port 1
//#define DHT11_PIN 4 // define analog port 1
long l;
byte read_dht11_dat()
{
byte i = 0;
byte result=0;
for(i=0; i< 8; i++)
{
while(!(PINC & _BV(DHT11_PIN)))
{}; // wait forever until anlog input port 0 is '1' (NOTICE: PINC reads all the analog input ports
//and _BV(X) is the macro operation which pull up positon 'X'to '1' and the rest positions to '0'. it is equivalent to 1<<X.)
delayMicroseconds(30);
if(PINC & _BV(DHT11_PIN)) //if analog input port 0 is still '1' after 30 us
result |=(1<<(7-i)); //this position is 1
while((PINC & _BV(DHT11_PIN))); // wait '1' finish
}
return result;
}
/*
const int powerpin = A0; // analog input pin 0 -- voltage
const int groundpin = A2; // analog input pin 2 -- ground
const int groundpin1 = A3; // analog input pin 2 -- ground
const int powerpin = A5; // analog input pin 0 -- voltage
const int groundpin = A3; // analog input pin 2 -- ground
const int groundpin1 = A2; // analog input pin 2 -- ground
*/
const int powerpin = A2; // analog input pin 0 -- voltage
const int groundpin = A4; // analog input pin 2 -- ground
void setup()
{
DDRC |= _BV(DHT11_PIN); //let analog port 0 be output port
PORTC |= _BV(DHT11_PIN); //let the initial value of this port be '1'
Serial.begin(9600);
Serial.println("Ready");
pinMode(groundpin, OUTPUT);
pinMode(powerpin, OUTPUT);
digitalWrite(groundpin, LOW);
digitalWrite(powerpin, HIGH);
// pinMode(groundpin1, OUTPUT);
// digitalWrite(groundpin1, LOW);
// digitalWrite(DHT11_PIN, HIGH); //pull up 2014.2.27.aht
Serial.println();
Serial.println();
Serial.println("pin_5g3v1i_DHT11_140311.aht");
Serial.println("28 A5");
Serial.println("27 groundpin = A4");
Serial.println("26 LM35 = A3");
Serial.println("25 powerpin = A2");
Serial.println("24 DHT11_PIN 1");
Serial.println("23 Vin = A0");
Serial.println("22 GND");
Serial.println();
analogReference(INTERNAL);
// ARDUINO UNO 와 연결 방법
// 2 번핀과 1번핀을 10K Ohm 저항으로 연결합니다.
// 1번핀 = 5V, 2번핀 = A0, 3번핀=연결하지 않음, 4번핀= 0V(GND)
//
// +---------------------+
// | |
// | |
// | DHT-11 |
// | |
// | |
// | |
// +---------------------+
// 1| 2| 3| 4|
// |-- | | |
// | \ | | |
// | / | | |
// 10K | \ | | |
// | / | | |
// | \ | | |
// | --| | |
// | | | |
// | | | |
// 5V A0 0V(GND)
}
void loop()
{
byte dht11_dat[5];
byte dht11_in;
byte i;// start condition
//
l++;
//Serial.print(l, DEC);
float ain;
Serial.println("");
Serial.print("0");
Serial.print(l, DEC);
Serial.print(": ");
ain = 0;
for (int ii=0; ii<1000; ii++) ain +=analogRead(A0);
ain = ain * 525.5 / 99.5 * 1.1 / 1024000 * 4.78 / 4.7;
Serial.print(ain);
Serial.print("V ");
ain = 0;
for (int ii=0; ii<1000; ii++) ain +=analogRead(A3);
ain = ain * 100 * 1.1 / 1024000;
Serial.print(ain);
Serial.print("C ");
ain = 0;
for (int ii=0; ii<10000; ii++) ain +=analogRead(A5);
ain = ain * 1.1 / 10240;
Serial.print(ain);
Serial.println("mV");
// Serial.print(float(analogRead(A1)) / 1.1 * 6.0);
// Serial.print(long(analogRead(A1)) * 600 / 11);
// print a tab between values:
PORTC &= ~_BV(DHT11_PIN); // 1. pull-down i/o pin for 18ms
delay(18);
PORTC |= _BV(DHT11_PIN); // 2. pull-up i/o pin for 40us
delayMicroseconds(1);
DDRC &= ~_BV(DHT11_PIN); //let analog port 0 be input port
delayMicroseconds(40);
dht11_in = PINC & _BV(DHT11_PIN); // read only the input port 0
if(dht11_in)
{
Serial.println("dht11 start condition 1 not met"); // wait for DHT response signal: LOW
delay(1000);
return;
}
delayMicroseconds(80);
dht11_in = PINC & _BV(DHT11_PIN); //
if(!dht11_in)
{
Serial.println("dht11 start condition 2 not met"); //wair for second response signal:HIGH
return;
}
delayMicroseconds(80);// now ready for data reception
for (i=0; i<5; i++)
{ dht11_dat[i] = read_dht11_dat();} //recieved 40 bits data. Details are described in datasheet
DDRC |= _BV(DHT11_PIN); //let analog port 0 be output port after all the data have been received
PORTC |= _BV(DHT11_PIN); //let the value of this port be '1' after all the data have been received
byte dht11_check_sum = dht11_dat[0]+dht11_dat[1]+dht11_dat[2]+dht11_dat[3];// check check_sum
if(dht11_dat[4]!= dht11_check_sum)
{
Serial.println("DHT11 checksum error");
}
/*
Serial.print("Current humdity = ");
Serial.print(dht11_dat[0], DEC);
Serial.print(".");
Serial.print(dht11_dat[1], DEC);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(dht11_dat[2], DEC);
Serial.print(".");
Serial.print(dht11_dat[3], DEC);
Serial.println("C ");
*/
Serial.print("1");
Serial.print(l, DEC);
Serial.print(": ");
Serial.print(dht11_dat[0], DEC);
Serial.print(".");
Serial.print(dht11_dat[1], DEC);
Serial.println("% ");
Serial.print("2");
Serial.print(l, DEC);
Serial.print(": ");
Serial.print(dht11_dat[2], DEC);
Serial.print(".");
Serial.print(dht11_dat[3], DEC);
Serial.println("C ");
//delay(2000); //fresh time
}
회원에게만 댓글 작성 권한이 있습니다.