In the last three posts I went through the basic steps to send data from an Android device to an Arduino using a bluetooth serial module. That was fun and all but it would be better if the Arduino could send data back to the Android device. With functionality like that there a lot more plausible uses for the technology. For this example I am going to go through the steps to make a 4 channel voltage logger but with a few minor changes this could be used to send/receive practically anything.

All files used are available on GitHub here.

Like last time, lets get the Arduino stuff out of the way first. For this project I have made a simple sketch and circuit that takes four analog inputs A0-A3 and then converts the values to 2dp float voltage values. The values are then displayed on the LCD screen and transmitted on the serial port (in our case, it is going to a Bluetooth module but for testing the serial port utility on the Arduino IDE will do for now). This process happens every 2000ms so that it is easy to follow what is happening. And just so we know that comms are being established both ways I have put in the ability to change the state of the built in LED on pin 13 on/off. Pretty standard wiring but here is a diagram incase you are unsure. I actually used a 4×20 LCD screen but the pin outs are usually the same or very similar and code is very easy to change for 2×16 etc. Also decided to use an Arduino Nano for this – love it!Screen Shot 2014-04-02 at 23.05.44Below is the sketch for the Arduino. It serves three simple functions:

1. Reads in sensor values from A0-A3 and converts them to float values in the range 0-5. So if 5V is put on A0, the sensor will be at 1023 which is converted with a mathematical function to 5.00.

2. Prints the converted values of the sensors on to an LCD screen. I used a 4×20  but if you use a smaller screen you should only need to change a few lines of code.

3. Sends converted sensor values over serial to another device, in this instance the bluetooth module. Using a set of print statements and a for loop the data string will look something like this #1.23+2.34+3.23+2.43+~ The ‘#’ and  ‘~’ are used by the Android app to determine the validity and length of the incoming string, as it knows ~ is the end. From here it can find the values of the four sensors using string methods. I used ‘+’ to separate the values for my own benefit, as it makes things clearer to see on hyperterminal etc, they aren’t needed for any other reason.


#include <LiquidCrystal.h>

int led = 13;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Pins used for inputs and outputs********************************************************
const int analogInPin0 = A0;// Analog input pins
const int analogInPin1 = A1;
const int analogInPin2 = A2;
const int analogInPin3 = A3;

//Arrays for the 4 inputs**********************************************
float sensorValue[4] = {0,0,0,0};
float voltageValue[4] = {0,0,0,0};

//Char used for reading in Serial characters
char inbyte = 0;
//*******************************************************************************************

void setup() {
  // initialise serial communications at 9600 bps:
  Serial.begin(9600);
  lcd.begin(20, 4); //change to 16, 2 for smaller 16x2 screens
  pinMode(led, OUTPUT);
  digitalWrite(led, HIGH);
}

void loop() {
  readSensors();
  getVoltageValue();
  printLCD();
  sendAndroidValues();
  //when serial values have been received this will be true
  if (Serial.available() > 0)
  {
    inbyte = Serial.read();
    if (inbyte == '0')
    {
      //LED off
      digitalWrite(led, LOW);
    }
    if (inbyte == '1')
    {
      //LED on
      digitalWrite(led, HIGH);
    }
  }
  //delay by 2s. Meaning we will be sent values every 2s approx
  //also means that it can take up to 2 seconds to change LED state
  delay(2000);
}

void readSensors()
{
  // read the analog in value to the sensor array
  sensorValue[0] = analogRead(analogInPin0);
  sensorValue[1] = analogRead(analogInPin1);
  sensorValue[2] = analogRead(analogInPin2);
  sensorValue[3] = analogRead(analogInPin3);
}
//sends the values from the sensor over serial to BT module
void sendAndroidValues()
 {
  //puts # before the values so our app knows what to do with the data
  Serial.print('#');
  //for loop cycles through 4 sensors and sends values via serial
  for(int k=0; k<4; k++)
  {
    Serial.print(voltageValue[k]);
    Serial.print('+');
    //technically not needed but I prefer to break up data values
    //so they are easier to see when debugging
  }
 Serial.print('~'); //used as an end of transmission character - used in app for string length
 Serial.println();
 delay(10);        //added a delay to eliminate missed transmissions
}

void printLCD()
{
  for (int i = 0; i<4; i++) //change 4 to 2 if using small screen
  {
    lcd.setCursor(0, i);
    lcd.write("Sensor");
    lcd.setCursor(7, i);
    lcd.print(i);
    lcd.setCursor(8, i);
    lcd.print(" = ");
    lcd.setCursor(11, i);
    lcd.print(voltageValue[i]);
    lcd.setCursor(15, i);
    lcd.print("V");
  }

}
void getVoltageValue()
{
  for (int x = 0; x < 4; x++)
  {
    voltageValue[x] = ((sensorValue[x]/1023)*5);
  }
}

Load that sketch on to your Arduino and power it all up. The voltage values should display on the screen if all ok. Put a lead from any of the pins A0-3 to 5V to check the sensors work correctly and the screen updates the results.  Momentarily disconnect the bluetooth module Tx and Rx from the Arduino and open the serial monitor in the Arduino IDE. You should see the voltage values being transmitted every 2 seconds in the form #1.23+1.54………~. Just send a 1 and a 0 back to the Arduino from the monitor to check the LED on pin 13 turns on/off and that is the Arduino side done and proved working. Mine looked like this:
IMG_1542

Not the neatest of things but it worked for me! On to the Android part in part 2.

Advertisements