Hardware Reference
In-Depth Information
Creating the Arduino sketch
Let's now build a simple sketch to test our sensor via Bluetooth. We are going to build a
sketch so that it measures the temperature and humidity from the DHT sensor when a given
command is received on the serial port.
We start by including the DHT library so that we can use the DHT11 sensor:
#include "DHT.h"
Then, define the sensor's pin and type:
#define DHTPIN 7
#define DHTTYPE DHT11
Note that if you are using a different DHT sensor, you will need to modify the code here.
For example, if you are using a DHT22 sensor, simply type the following:
#define DHTTYPE DHT22
You will also need to define an instance of the DHT sensor and to send the sensor type and
sensor pin as an argument:
DHT dht(DHTPIN, DHTTYPE);
In the setup() function of the sketch, you will have to initialize the DHT sensor's in-
stance and start the serial connection:
dht.begin();
Serial.begin(115200);
We need to use a speed of 115,200 bauds here instead of the standard 9600 bauds, as it is
recommended in the specifications of the Bluetooth module.
Then, in the loop() function, we are going to constantly check the status of the serial port
to see whether a command was received from the computer:
if (Serial.available()) {
Search WWH ::




Custom Search