Combining three gadgets and I’ve acquired recently, this “mashup” (or rather, mash-together, or plug-in together… whichever) combines a few spare parts I had laying around. I figured this would be a good one-day project, to take a break from my other studies.
Pictured above is a breadboard with three main components: An arduino mini, SHT11 temp/humidity sensor, and a BlueSMiRF Bluetooth modem. It’s a bit overkill for a wireless temperature sensor, but like I said, I felt like throwing together something simple using some spare parts, and figured that this project would not take too long to assemble and write code for.
Hardware
In the pictures above, you can see generally how things are connected. I would have been a bit more tidy and used a smaller breadboard, but I did not have one. That long green wire is routed to the 9v supply line, and can be unplugged to attach an arduino NG, to be used to program the mini. Instructions for doing this can be found here.
The board above is powered by a 9v battery, and with an alkaline cell lasts about eight hours. The bluesmirf is hooked up the same as was in my original test for this device (where I hooked it up to an arduino NG). That is, ground and 5v (of course), and Rx to Tx, Tx to Rx. Note that I am powering the arduino with 9v, and drawing 5v from the output of the mini, which is using the mini’s voltage converter. This seems to work okay and I have not experienced any problems with current consumption.
The SHT11 is hooked up similarly: ground, 5v; the data pin is connected to the arduino’s pin 10, and the clock connected to pin 11. These are internal numbers: 10 and 11 correspond to pins 13 and 14 on the mini’s DIP headers.
Additionally, I hooked up an LED to pin 13 for a status indicator. In the pictures you might notice that there are a few jumper blocks that I have wires soldered to. This is because some of the wires are hard to push through the breadboard’s holes. Like I said, this isn’t perfect, organized, or pretty, but it gets the job done for the purposes of demonstrating the use of both the humidity/temperature sensor, and the Bluetooth modem.
Software
There are two parts of code required to make use of this hardware: the firmware (which will be uploaded to the arduino), and the PC software (which will connect to the device, receive data, and record it in a log file. Connecting to the bluesmirf is explained on this page. There is also code to use the SHT11 on this page.
Arduino sketch
long logTime; // For counting with millis()
int adcCount;
int shtClk=11; // Common clock for both SHT11's
int shtData=10; // Selectable data pin
int ioByte;
int ackBit;
unsigned int retVal; // Return value from SHT11
int dly;
long msTimer;
int k;
int cmdByte;
int dataByte;
uint8_t bitmask;
void SHT_Write_Byte(void) {
pinMode(shtData, OUTPUT);
shiftOut(shtData, shtClk, MSBFIRST, ioByte);
pinMode(shtData, INPUT);
digitalWrite(shtData, LOW);
digitalWrite(shtClk, LOW);
digitalWrite(shtClk, HIGH);
ackBit = digitalRead(shtData);
digitalWrite(shtClk, LOW);
}
int shiftIn() {
int cwt;
cwt=0;
bitmask=128;
while (bitmask >= 1) {
digitalWrite(shtClk, HIGH);
cwt = cwt + bitmask * digitalRead(shtData);
digitalWrite(shtClk, LOW);
bitmask=bitmask/2;
}
return(cwt);
}
void SHT_Read_Byte(void) {
ioByte = shiftIn();
digitalWrite(shtData, ackBit);
pinMode(shtData, OUTPUT);
digitalWrite(shtClk, HIGH);
digitalWrite(shtClk, LOW);
pinMode(shtData, INPUT);
digitalWrite(shtData, LOW);
}
void SHT_Connection_Reset(void) {
shiftOut(shtData, shtClk, LSBFIRST, 255);
shiftOut(shtData, shtClk, LSBFIRST, 255);
}
void SHT_Soft_Reset(void) {
SHT_Connection_Reset();
ioByte = 30;
ackBit = 1;
SHT_Write_Byte();
delay(15);
}
void SHT_Wait(void) {
delay(5);
dly = 0;
while (dly < 600) {
if (digitalRead(shtData) == 0) dly=2600;
delay(1);
dly=dly+1;
}
}
void SHT_Start(void) {
digitalWrite(shtData, HIGH);
pinMode(shtData, OUTPUT);
digitalWrite(shtClk, HIGH);
digitalWrite(shtData, LOW);
digitalWrite(shtClk, LOW);
digitalWrite(shtClk, HIGH);
digitalWrite(shtData, HIGH);
digitalWrite(shtClk, LOW);
}
void SHT_Measure(int vSvc) {
SHT_Soft_Reset();
SHT_Start();
ioByte = vSvc;
SHT_Write_Byte();
SHT_Wait();
ackBit = 0;
SHT_Read_Byte();
int msby;
msby = ioByte;
ackBit = 1;
SHT_Read_Byte();
retVal = msby;
retVal = retVal * 0x100;
retVal = retVal + ioByte;
if (retVal <= 0) retVal = 1;
}
int SHT_Get_Status(void) {
SHT_Soft_Reset();
SHT_Start();
ioByte = 7;
SHT_Write_Byte();
SHT_Wait();
ackBit = 1;
SHT_Read_Byte();
return(ioByte);
}
void SHT_Heater(void) {
SHT_Soft_Reset();
SHT_Start();
ioByte = 6;
SHT_Write_Byte();
ioByte = 4;
SHT_Write_Byte();
ackBit = 1;
SHT_Read_Byte();
delay(500);
SHT_Soft_Reset();
SHT_Start();
ioByte = 6;
SHT_Write_Byte();
ioByte = 0;
SHT_Write_Byte();
ackBit = 1;
SHT_Read_Byte();
}
void setup()
{
pinMode(11, OUTPUT);
digitalWrite(11, HIGH);
pinMode(10, OUTPUT);
pinMode(13, OUTPUT);
Serial.begin(115200); // open serial
SHT_Connection_Reset();
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
}
void loop()
{
while (Serial.available() > 1) {
cmdByte = Serial.read();
dataByte = Serial.read();
switch (cmdByte) {
case 65: // C
{
if (dataByte == 66) k=1;
break;
}
case 68:
{
k=0;
break;
}
}
}
if (k==1) {
msTimer=millis();
if (msTimer <= logTime) {
logTime = 0;
}
if (msTimer > (logTime + 1000)) {
logTime = millis();
// SHT11 #1 Temperature
SHT_Measure(3);
Serial.print(" T ");
Serial.print(retVal, DEC);
Serial.print(" H ");
// SHT11 #1 Humidity
SHT_Measure(5);
Serial.print(retVal, DEC);
Serial.println();
}
}
}
Sample Output
After seding the lettes “AB” to the MCU, the sensor values will be returned once per second, and should look like this:
T 5995 H 1290
T 5994 H 1270
T 5994 H 1241
T 5994 H 1232
Note that since the MCU is looking for two characters, one could send “ABxABx” to handle both possible situations. D is there because when the bluesmirf disconnects, it sends “DISCONNECT” to the MCU. When the PC is not connected, having the microcontroller send data seems to cause the bluetooth modem not to work. Keep this in mind. Code should be made to only return data on request.