Nov 152013
 

I haven’t had time to post anything on here for a while now. Have been busy with a bunch of other things.

The first meetup “Essential Arduino – Make, program and interface!” was a success! I had a bunch of bright folks attend the workshop and i am planning on hosting more of these in the near future.

See the link for messages, feedback and pictures:

http://www.meetup.com/BayArea-Arduino/events/144769502/

Essential Arduino - Make, program and interface!

Essential Arduino – Make, program and interface!

May 312013
 

In my last tutorial i explained how to hook up a 1-wire  temperature sensor (DS18B20) to Arduino. In this tutorial we will add on to that work by creating a temperature logger using a SD card breakout and the DS18B20 sensor. We will store the temperature values as a new lines in a text file every few seconds on the SD card.

The SD card breakout that i will be using for this tutorial is from  from Shenzhen LC Technology Co.,Ltd. and can be bought on eBay for less than $5. It’s a pretty inexpensive way to add substantial storage capacity to your Arduino project.

Search for “SD card module arduino” on eBay and you would want to get a breakout that looks like this:

LC-STUDIO-SD-CARD-MODULE.FRONT LC-STUDIO-SD-CARD-MODULE.BACK

 

 

 

 

 

Follow my last tutorial to hookup the DS18B20 sensor (you’d need a breadboard for this). Now, refer to this table to hookup the SD card breakout to Arduino:

SD Card Breakout Arduino Description
5V 5V VDD
GND GND Ground
CS 10 Carrier Sense
MOSI 11 Also known as DI
MISO 12 Also known as DO
SCK 13 Also known as CLK

My setup looks something like this (click on image to see a bigger size)

Temperature Logger Arduino

 

 

 

 

 

 

Let’s look at the sketch that gets the temperature data from the SD card and logs it into a text file every few seconds.

The first thing that you’d need to do is to include the SD library (SD.h) in addition to the OneWire library. In setup() we use the pinMode function to set the pin 10 as an output pin.

SD.begin should return true if everything was OK in your setup and a valid SD card was recognized (make sure you have the write protect tab turned to off position)

The setupDS1820 function sets up the 1-wire communication. The gettemp() function parses the temperature data from the sensor and constructs a nicely formed string with the temperate in both Fahrenheit and Celsius. In the loop() function we use SD.open(“templog.txt”, FILE_WRITE) function call to open a file(creates one if it doesn’t exist on the card) in WRITE mode and write the temperature to it every 5 seconds.

#include <OneWire.h>
#include <SD.h>

OneWire ds(2); // on pin 2 (a 4.7K resistor is necessary)
byte addr[8];

void setup(void) {
Serial.begin(9600);
pinMode(10, OUTPUT);

if (!SD.begin(10)) {
Serial.println(“SD card initialization failed!\n”);
return;
}
Serial.println(“SD card initialization done.\n”);

setupDS1820();
}

void setupDS1820(void) {

byte i;
byte present = 0;
byte type_s;
byte data[12];
float celsius, fahrenheit;

if ( !ds.search(addr)) {
Serial.println(“No more addresses.”);
Serial.println();
ds.reset_search();
delay(250);
return;
}

Serial.print(“ROM =”);
for( i = 0; i < 8; i++) {
Serial.write(‘ ‘);
Serial.print(addr[i], HEX);
}

if (OneWire::crc8(addr, 7) != addr[7]) {
Serial.println(“CRC is not valid!”);
return;
}
Serial.println();

// the first ROM byte indicates which chip
switch (addr[0]) {
case 0×10:
Serial.println(“Chip = DS18S20\n”); // or old DS1820
type_s = 1;
break;
case 0×28:
Serial.println(“Chip = DS18B20\n”);
type_s = 0;
break;
case 0×22:
Serial.println(“Chip = DS1822\n”);
type_s = 0;
break;
default:
Serial.println(“Device is not a DS18x20 family device.\n”);
return;
}
}

String gettemp(void) {

byte i;
byte present = 0;
byte type_s;
byte data[12];
float celsius, fahrenheit;

ds.reset();
ds.select(addr);
ds.write(0×44, 1); // start conversion, with parasite power on at the end

delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.

present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad

for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}

// Convert the data to actual temperature
// because the result is a 16 bit signed integer, it should
// be stored to an “int16_t” type, which is always 16 bits
// even when compiled on a 32 bit processor.
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0×10) {
// “count remain” gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 – data[6];
}
}
else {
byte cfg = (data[4] & 0×60);
// at lower res, the low bits are undefined, so let’s zero them
if (cfg == 0×00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0×20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0×40) raw = raw & ~1; // 11 bit res, 375 ms
//// default is 12 bit resolution, 750 ms conversion time
}
celsius = (float)raw / 16.0;
fahrenheit = celsius * 1.8 + 32.0;
char fChar[5];
char cChar[5];
char tempStr[100];
dtostrf(fahrenheit, 3,1,fChar);
dtostrf(celsius, 3,1,cChar);
memset(tempStr, 0, 100);
strcat(tempStr, “Temperature : “);
strcat(tempStr,fChar);
strcat(tempStr, ” Fahrenheit, “);
strcat(tempStr,cChar);
strcat(tempStr,” Celsius”);
return tempStr;
}

void loop(void) {
File dataFile = SD.open(“templog.txt”, FILE_WRITE);
Serial.println( gettemp());
if (dataFile) {
dataFile.println(gettemp());
dataFile.close();
delay(5000);
} else {
Serial.println(“error opening templog.txt”);
}

}

 

May 312013
 

In this tutorial i will teach you how to hook up an inexpensive DS18B20 temperature sensor with Arduino. This can be ordered for around $4 – $5 from Digikey, Mouser or several other online retailers.

Before we get started on this; we should understand the 1-Wire communications bus system developed by Dallas Semiconductor. Basically, this protocol is similar to I2C (don’t bother if you are not familiar with it) in that it uses a low-data rate, signaling and power over a single signal (wire). Each device on the bus has a unique 64-bit serial number and the bus is governed by a master which in our case is an Arduino (microcontroller).

The master starts a transmission with a reset pulse, which pulls the wire to 0 volts for at least 480 µs. This resets every slave device on the bus. After that, any slave device, if present, shows that it exists with a “presence” pulse: it holds the bus low for at least 60 µs after the master releases the bus. To send a “1″, the bus master software sends a very brief (1–15 µs) low pulse. To send a “0″, the software sends a 60 µs low pulse.

Now that you have your basics clear; let’s start hooking this up to Arduino. The pinouts for DS18B20 look like this:

ds18b20-pins

 

 

 

 

 

 

 

DS18B20 accepts both 3.oV and 5.5V for power. So we can hook up GND, VDD pin to Arduino to power this thing up. Even better, since this is a 1-Wire bus system it can power itself from the data line so we will only use 2 lines; GND and DQ!

We will start by connecting GND and VDD pins to GND on Arduino. We will use digital pin 2 on Arduino to connect to the DQ pin for data. We will also connect a 4.7K pullup resistor on the DQ pin to pull it up to 5V.

Your setup should look like this (click image for a larger size)

DS18B20-Arduino

 

 

 

 

 

 

Now let’s load up our sketch on Arduino.

#include <OneWire.h>

OneWire ds(2); // on pin 10 (a 4.7K resistor is necessary)

void setup(void) {
Serial.begin(9600);
}

void loop(void) {
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit;

if ( !ds.search(addr)) {
Serial.println(“No more addresses.”);
Serial.println();
ds.reset_search();
delay(250);
return;
}

Serial.print(“ROM =”);
for( i = 0; i < 8; i++) {
Serial.write(‘ ‘);
Serial.print(addr[i], HEX);
}

if (OneWire::crc8(addr, 7) != addr[7]) {
Serial.println(“CRC is not valid!”);
return;
}
Serial.println();

// the first ROM byte indicates which chip
switch (addr[0]) {
case 0×10:
Serial.println(” Chip = DS18S20″); // or old DS1820
type_s = 1;
break;
case 0×28:
Serial.println(” Chip = DS18B20″);
type_s = 0;
break;
case 0×22:
Serial.println(” Chip = DS1822″);
type_s = 0;
break;
default:
Serial.println(“Device is not a DS18x20 family device.”);
return;
}

ds.reset();
ds.select(addr);
ds.write(0×44, 1); // start conversion, with parasite power on at the end

delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.

present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad

Serial.print(” Data = “);
Serial.print(present, HEX);
Serial.print(” “);
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
Serial.print(data[i], HEX);
Serial.print(” “);
}
Serial.print(” CRC=”);
Serial.print(OneWire::crc8(data, 8), HEX);
Serial.println();

// Convert the data to actual temperature
// because the result is a 16 bit signed integer, it should
// be stored to an “int16_t” type, which is always 16 bits
// even when compiled on a 32 bit processor.
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0×10) {
// “count remain” gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 – data[6];
}
} else {
byte cfg = (data[4] & 0×60);
// at lower res, the low bits are undefined, so let’s zero them
if (cfg == 0×00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0×20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0×40) raw = raw & ~1; // 11 bit res, 375 ms
//// default is 12 bit resolution, 750 ms conversion time
}
celsius = (float)raw / 16.0;
fahrenheit = celsius * 1.8 + 32.0;
Serial.print(” Temperature = “);
Serial.print(celsius);
Serial.print(” Celsius, “);
Serial.print(fahrenheit);
Serial.println(” Fahrenheit”);
}

Fire up the serial monitor in your Arduino IDE and you should see some like this.

 

May 212013
 

I can’t wait to get my hands on the new 2 layer Arduino clone (boarduino) PCBs that i have ordered from OSH Park. They are in fab right now and should get to me by the end of this month. I plan on creating several sensor based projects using these boards. I have also ordered parts from Mouser. For those of you who order via Digi-key; i found Mouser’s prices to be more competitive. I have posted the part list and prices below.

Here are the renders of the eagle files uploaded to OSH:

Boarduinothumb_i

 

 

Mouser price list:

part-list

May 212013
 

Added support for area selection and status bar. Source posted on Github.

https://github.com/goravtaneza/jcapture

Click below to launch via Java Webstart.