A simple Android <-> Arduino Bluetooth project

This is a simple project that use a bluetooth channel in order to connect an Android application with an Arduino.
The aim of this sample is:

  • Learn how Bluetooth serial channel works
  • Learn how to configure a bluetooth device on Android
  • Send a string from Arduino to Android (in this example the value of light sensor)
  • Send a byte from Android to Arduino (in this example the LED ON-OFF status)

The Arduino schema is very simple

schema_bb

 

I’ve used an HC-06 bluetooth adapter, the cheapest (and slower) available on ebay. You can find the source code of the android application and Arduino Sketch file on my GITHUB repositories.

Now I’m ready to add bluetooth feature on my “HomeTemp” project.

 

Bluetooth HC-06 + Arduino + RX1 and TX1

Yes…I’m alive.
It’s about 1 year that I didn’t post something on this wonderful blog but I’ve been quite busy.

But some days ago I bought a bluetooth module (HC-06, the cheapest module for Arduino) in order to integrate wireless capability in my HomeTemp project, and I want to share a simple thing about its configuration:

DO NOT WIRE THE MODULE TO RX1 (pin 0) AND TX1 (pin 1).

I tried the easiest way to connect the module to an Arduino UNO board, using the standard RX1 and TX1 pin, but the behavior was a little confusing:

  • The module sends correctly data via bluetooth
  • The module does not receive any data via bluetooth

I didn’t understand why…maybe a “conflict” with the standard Serial USB communication. But after some experiments  I found a post somewhere that suggests to use the “SoftwareSerial” Arduino Library and use two others PIN (10 and 11). With this simple configuration the following example works like a charm 🙂 Not all pins support change interrupts, check the SoftwareSerial page and check which pins should work.

Pair the device with your PC (the default pairing PIN is 1234) and then open a serial console (on Mac OSX I use “screen /dev/tty.XXXYYZZZ). You are now able to read and send data.

I hope it could be helpful to someone.


#include <SoftwareSerial.h>
#define LED_PIN 12

SoftwareSerial bluetoothSerial(10, 11);

void setup() {
  pinMode(LED_PIN, OUTPUT);  

  
  Serial.begin(57600);

  bluetoothSerial.begin(9600);
  bluetoothSerial.println("I'm ready");
  
  digitalWrite(LED_PIN, LOW);
  delay(300);
  digitalWrite(LED_PIN, HIGH);
  delay(300);
  digitalWrite(LED_PIN, LOW);
  delay(300);
  digitalWrite(LED_PIN, HIGH);
  delay(300);
  digitalWrite(LED_PIN, LOW);
  
  Serial.print("Ready to go");  

}

void loop() {

  if (bluetoothSerial.available() > 0) {
    char inChar = bluetoothSerial.read();
    if (inChar == '1') {
        digitalWrite(LED_PIN, HIGH);
        bluetoothSerial.print("ON");
    }
    else if (inChar == '0'){
        digitalWrite(LED_PIN, LOW);
        bluetoothSerial.print("OFF");
    }
  } 

}

CanonTimer Version 1

Welcome to my first Arduino DIY project. I need a intervalometer in order to do some time-lapse video. On amazon or ebay you can find a standard timer for less than 40€ but I want to build it by myself 🙂

A lot of DIY timer can be discovered using Google, but this is too simple. So I bought an Arduino Uno, some electronics parts, and now I can share my CanonTimer project with the first version!

SchemaYou can found the Arduino code and schematics on my Bitbuchet GIT repository.

In this first version the user interface is very simple:  there are nine hardcoded delays assigned to nine different programs

  • 5 seconds – Program 0
  • 10 seconds – Program 1
  • 15 seconds – Program 2
  • 30 seconds – Program 3
  • 45 seconds – Program 4
  • 60 seconds – Program 5
  • 90 seconds – Program 6
  • 120 seconds – Program 7
  • 180 seconds – Program 8
  • 300 seconds – Program 9

You can select the time delay using the right button. Once the program is selected, you can start the timer using the left button. Every XX seconds the led will blink.

Yes…the led…in this first version there isn’t a real connection to a Canon Camera. In the upcoming releases I will introduce some other improvements:

  • Wired connection to the Canon Camera 🙂
  • A more complete user interface, without hardcoded time delays but with a 4 digit 7-segment display and (+) and (-) buttons in order to specify a custom delay

 

Welcome back, and happy new year!

I’m back…and I’m back with a lot of news 🙂

  • First of all, I changed the website hosting. After some years of (free) web hosting I had to search for another solution for this site and for another two blogs that I manage. (www.asvirtus.it and www.bandacastanoprimo.it).  My choice was the Shared Hosting by DreamHost.com. Cheap, no limits, fast setup, easy control panel….so perfect to me!
  • After four months waiting for the 9$ Arduino Board (my shipment is lost, I think) I decided to buy an Arduino Uno on Ebay for a new small project…more news coming soon 🙂Schema
  • I published a new little Android software on Google Play, “Webcam Valle d’Aosta”: a very simple application that shows the main webcam of Italian Alps (no english translation, only italian)
  • The last news is…my new Laptop.  After 5 years I said “GoodBye” to my old Sony Vaio and I’ve joined the “Apple” side with a beautiful and powerful Mac©®™ Book Pro Retina 13”. Yes….I’ve always been an Apple enemy and a great fanboy of OpenSource and Linux. But now I’m old…and bored of spending my time configuring my laptop at every “apt-get upgrade”. A new laptop with Windows 8 is the worst thing, so I chose a “working and easy to use” version of Linux: Mac®™© OS-X. I can do everything with it: coding and programming (arduino, android, cyanogenMod), photo editing, playing with my favorites games (Diablo III for example works like a charm on my Retina©™® display) etc etc etc…Yes…the MBP-Retina is a little bit expensive (with expanded RAM – 16GB ), but this little boy is, simple, perfect.

So…happy new year and stay tuned for the next news 🙂

Thermistor sample code for Arduino – Raspberry

This is a C++ class for a simple NTC thermistor. I used the thermistor included in the cooking-hacks.com starter kit (datasheet) and the cooking-hacks.com Raspebby to Arduino shield, but this sample is easily adaptable for any NTC thermistor or also for Arduino platform. It can be used with another NTC thermistor: simply change the BasicThermistor.h values:

  • BETA (o B-Value) : the beta value of thermistor
  • THERMISTOR: zero power resistance at 25° C
BasicThermistor schema
BasicThermistor schema

Some notes:

  • This class is designed for a read every 10 seconds.
  • Check the real value of 10k resistance with a multimeter! The 10% tollerance changes the value of about ± 1° C!. In my example I use 9840 Ω instead of 10000 Ω
  • I use a arithmetic mean over 12 readings (2 minutes) in order to normalize the temperature value and discard read errors
  • I use a simple “is_valid_value” function that discard a macroscopic read error. A “digital” read error of ± 20 is converted to an error of ± 14° C ! So if the previous valueis too big or too small, discard the current value

Header file – BasicThermistor.h

/*
 * BasicThermistor.h
 *
 * Created on: 07/ago/2013
 * Author: sarbyn
 * Website: http://www.sarbyn.com
 */
#ifndef BASICTHERMISTOR_H_
#define BASICTHERMISTOR_H_

#include &quot;arduPi.h&quot;
#include

/**
 * Based on cooking-hacks NTC thermistor - datasheet available at
 * http://www.cooking-hacks.com/skin/frontend/default/cooking/pdf/159-282-86001.pdf
 */

// The real 10k resistor value
#define TENKRESISTOR 9840.0f
// Thermistor beta value (from datasheet)
#define BETA 3950.0f
// The thermistor value at room temp (25 C)
// the original value was 2800 ohm
#define THERMISTOR 2700.0f
// The room temperature (25 C) in kelvin
#define ROOMTEMPK 298.15f

// Number of readings over which calculate
// the temperature
#define READINGS 12
// Delay (in ms) between Wire operations
#define DELAY_BETWEEN_OPERATION 20

// The digital value readed from ADC
// must be lower than DIGITAL_TOLLERANCE
// Otherwise it is treaded as a read error
#define DIGITAL_TOLLERANCE 20
// If the digital value readed from ADC is wrong for
// more than DIGITAL_TOLLERANCE_COUNTER, it is a correct value
// and so use it. This is used in case of sudden
// temperature change. If the temperature changes suddenly,
// the value is discarded for DIGITAL_TOLLERANCE_COUNTER reads. After that
// the value is accepted
#define DIGITAL_TOLLERANCE_COUNTER 6

#define ADC_I2C_ADDRESS 8

class BasicThermistor {
public:
   BasicThermistor(byte i2c_address, boolean debug_flag);
   float read_temperature();
private:
   byte address;
   boolean debug;
   int digital_value;
   float read_buffer[READINGS];
   unsigned int cached_digital_value;
   float temperature;
   int invalid_read_counter;
   int buffer_pointer;

   int read_digital_value();
   int is_valid_value(int value);
   float calc_temperature(int value);
};

#endif /* BASICTHERMISTOR_H_ */

Class file – BasicThermistor.cpp

/*
 * BasicThermistor.cpp
 *
 * Created on: 07/ago/2013
 * Author: sarbyn
 * Website: http://www.sarbyn.com
 *
 */

/**
 * Based on cooking-hacks NTC thermistor - datasheet available at
 * http://www.cooking-hacks.com/skin/frontend/default/cooking/pdf/159-282-86001.pdf
 */

#include &quot;BasicThermistor.h&quot;

// Costructor
BasicThermistor::BasicThermistor(byte i2c_address, boolean debug_flag) {
  address = i2c_address;
  debug = debug_flag;
  temperature = 0.0;
  cached_digital_value = 0;
  digital_value = 0;
  invalid_read_counter = 0;
  buffer_pointer = 0;

  for (int i = 0; i &lt; READINGS; i++) {
    read_buffer[i] = -1.0f;
  }
}
// Read the digital value from ADC using Wire class
int BasicThermistor::read_digital_value() {
  unsigned char val_0 = 0;
  unsigned char val_1 = 0;
  unsigned int digital_value;
  Wire.beginTransmission(ADC_I2C_ADDRESS);
  Wire.write(address);
  // sleep in order to reduce read error
  delay(DELAY_BETWEEN_OPERATION);
  Wire.requestFrom(ADC_I2C_ADDRESS, 2);
  // sleep in order to reduce read error
  delay(DELAY_BETWEEN_OPERATION);
  val_0 = Wire.read();
  // sleep in order to reduce read error
  delay(DELAY_BETWEEN_OPERATION);
  val_1 = Wire.read();
  // sleep in order to reduce read error
  delay(DELAY_BETWEEN_OPERATION);
  digital_value = int(val_0)*16 + int(val_1&gt;&gt;4);

  if (debug) {
    printf(&quot;[BT] val [%2x,%2x]\n&quot;, val_0, val_1);
    printf(&quot;[BT] Read digital value %d\n&quot;, digital_value);
  }
  return digital_value;
}

// Check if the readed value is valid
int BasicThermistor::is_valid_value(int value) {
  if (cached_digital_value == 0) {
    cached_digital_value = value;
    return 1;
  }

  // check against DIGITAL_TOLLERANCE
  if (abs(cached_digital_value - value) &lt; DIGITAL_TOLLERANCE) {
    if (debug) printf(&quot;[BT] Valid digital value %d\n&quot;, value);
    cached_digital_value = value;
    invalid_read_counter = 0;
    return 1;
  }
  if (invalid_read_counter &gt; DIGITAL_TOLLERANCE_COUNTER) {
    // too many invalid readings ---&gt; this is a valid read
   if (debug) printf(&quot;[BT] Too many invalid digital value %d..so use it\n&quot;, value);
   cached_digital_value = value;
   invalid_read_counter = 0;
   return 1;
 }

  // throw away invalid reading
  invalid_read_counter++;
  if (debug) printf(&quot;[BT] Invalid digital value %d\n&quot;, value);
  return 0;
}

// Convert the ADC value to Celsius Temperature
float BasicThermistor::calc_temperature(int value) {
  // convert value
  float analog_value = value * 1023.0 / 4095.0;

  analog_value = (1023 / analog_value) - 1;
  analog_value = TENKRESISTOR / analog_value;
  analog_value = analog_value / THERMISTOR;

  if (debug) printf(&quot;[BT] Analog value %f\n&quot;, analog_value);

  float kelvin = log(analog_value);
  kelvin /= BETA;
  kelvin += 1.0 / ROOMTEMPK;
  kelvin = 1.0 / kelvin;

  return kelvin - 273.15;
}

// Main read function: read the current temperature and return it.
// This is designed for a read every 10 seconds: it collects 12 readings
// (READINGS macro value, 2 minutes) and calculate mean over all the values.
// In this way the temperature value does not change suddenly and
// become more stable
float BasicThermistor::read_temperature() {
  digital_value = read_digital_value();
  float single_temperature;
  if (is_valid_value(digital_value)) {
    single_temperature = calc_temperature(digital_value);
    if (debug) printf(&quot;[BT] Single current temperature is %f\n&quot;, single_temperature);
  } else {
    single_temperature = calc_temperature(cached_digital_value);
    if (debug) printf(&quot;[BT] Invalid temp, using cached value %f\n&quot;, single_temperature);
  }

  if (debug)
    printf(&quot;[BT] Storing the temp at pointer %d\n&quot;, buffer_pointer);

  read_buffer[buffer_pointer] = single_temperature;
  buffer_pointer++;

  if (read_buffer[buffer_pointer] == -1.0f) {
    // this is the first reading....init the buffer
    for (int i = buffer_pointer; i &lt; READINGS; i++) {
      read_buffer[i] = single_temperature;
    }
  }

  // reset the pointer
  if (buffer_pointer == READINGS) buffer_pointer = 0;

  temperature = 0;

  if (debug) {
    printf(&quot;[BT] Temp. buffer[&quot;);
    for (int i = 0; i &lt; READINGS; i++) {
      printf(&quot;%0.1f,&quot;, read_buffer[i]);
    }
    printf(&quot;]\n&quot;);
  }

  for (int i = 0; i &lt; READINGS; i++) {
    temperature += read_buffer[i];
  }

  temperature /= READINGS;

  return temperature;
}

A $9 arduino board

Some days ago I found on indiegogo.com this campaign:

$9 ARDUINO Compatible STARTER KIT

The goal of the founder is to reach 12.000$ and start the production of the Arduino-like board, and now the campain is over 73.000$! . With 12$ (9$ + shipping), or about 9€, you can start to play with Arduino and electronics. I’ve never tried Arduino boards and yesterday I decided to bought one starter kit from indiegogo.com, so in September my little Raspberry Pi will have a new little friend 🙂