Hardware Reference
In-Depth Information
In the loop() function, we are going to continuously read data from the sensors, and print
it on the Serial port. It starts by getting data from the temperature & humidity sensor:
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
For the photocell, we first read data from the analog pin A0, which returns a value from
0 to 1023, at the resolution of the Analog-To-Digital converter of the Arduino Uno board
is 10 bits, so 1024 values. Then, we divide this reading by 1024 and multiply it by 100 to
have the light level as a percentage:
float sensor_reading = analogRead(A0);
float light = sensor_reading /1024*100 ;
Then, we print these different measurements on the Serial port. First, the temperature:
Serial.print( "Temperature: " );
Serial.print(( int )temperature);
Serial.print(( char ) 223 );
Serial.println( "C" );
The humidity is exactly similar, just as the light level:
Serial.print( "Light: " );
Serial.print(light);
Serial.println( "%" );
Finally, we introduce a delay of 500 ms between each new set of measurements:
delay( 500 );
Note that the complete code for this chapter can be found on the corresponding folder in-
side the GitHub repository of the topic:
https://github.com/openhomeautomation/iot-book
Search WWH ::




Custom Search