Android Bluetooth serial port communication

Tablets and smartphones are everywhere and prices are dropping fast.  A tablet provides a great user interface: it’s inexpensive, has a high resolution color touchscreen and it’s an ideal method to control an embedded system.

The most straightforward way to do this is with an embedded system exposing a web  interface over Wi-Fi. In this case, the tablet only needs a browser to connect. However, smaller embedded systems may not have this luxury. Here we will look at using an Android tablet to connect to a small embedded system using Bluetooth.

We won’t get into the details of the embedded system but for clarity’s sake, let’s say it’s a small Arduino measuring room temperature and connected to a Bluetooth transmitter. It sends a reading automatically once per second.

There is a lot of information online about using Bluetooth with the Android. The problem is that it is fragmented and few sites have all the information in one place. So I figured I’d compile a set of the major points you need to know to get the Bluetooth Serial Port Protocol (SPP) working on an Android app.

First, your app needs the BLUETOOTH and BLUETOOTH_ADMIN permissions. This goes in yourAndroidManifest.xml file.

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

Now that we have given the app permission to access the Bluetooth API, let’s look at the classes that are relevant. We’ll need the BluetoothAdapter, BluetoothDevice and BluetoothSocket classes to connect to the external system.

First, we need to find and pair with the device. The following code snippet builds a collection of Bluetooth devices that were discovered by your Android device.

    BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter();
    bta.startDiscovery();
    Set deviceList = bta.getBondedDevices();

Now that we have a list of external devices, we can iterate over the collection and extract the name and address of each device like this.

    for (BluetoothDevice i : deviceList)
    {
        String name = i.getName();
        String address = i.getAddress();
    }

You can select which one you need from the list and connect to it. Selecting the device is something you can decide how to handle yourself. Connecting to the device is done using its Bluetooth address. Once you have a selected device, we connect to it by requesting the device through its address. The address is a unique 48-bit ID assigned to each Bluetooth device.
Once we have the device, then we’ll open a socket using the standard UUID for the SPP serial port protocol to indicate that we want to connect to the device as if it were a standard serial port.

    BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter();
    BluetoothDevice device = bta.getRemoteDevice(address);
    BluetoothSocket socket = null;
    if (device != null)
    {
        UUID serialID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
        try
        {
            socket = device.createRfcommSocketToServiceRecord(serialID);
        }
        catch (java.io.IOException e)
        {
        }
    }

Next, we’ll handle sending and receiving data from the socket.

Trimmable setpoint voltage divider

Let’s say you’re using a comparator like the venerable old LM339. You provide a setpoint “reference” and an input. If the input is below the setpoint, the output is clamped to ground. If the output is above the setpoint (plus any offset voltage of the comparator, of course), the open-collector output floats. Normally we tie the output to +5V if we want a TTL level output as we are often using the comparator to send a signal to a digital or microcontroller circuit. With me so far? OK.

The classic voltage divider is a good choice for a setpoint if it only needs to stay constant. But what if you need a variable setpoint? Simple, use a trimmer, trimpot, variable potentiometer, whatever you wanna call it. Now you can change the voltage of the setpoint. But wait a minute. If we have a regulated 12VDC and use a trimpot, even an expensive multiturn, it can still be difficult to set that voltage to within a millivolt. After all, a 10-turn pot with 12V at the input is still 1.2 volt per revolution, so with 360 degrees per revolution, you’d need fingers precise to almost 1/3 of a degree to set it to within a millivolt. Possible, but difficult.

Like most things, there are multiple ways to skin this particular cat (I can only write this while Lefty and Poncho are not in the room…).

20150424_194031

In the olden, golden days, engineers with beards and slide-rules used verniers that geared the output down, so one turn of the knob might only be 1/10th turn of the potentiometer on the output.
vernier

This makes it easier to adjust, but those things are $$$. Gotta be a cheaper solution. Sure, use a voltage divider. Remember the divider can take an input voltage and give you a smaller output, but if we make the divider variable, we can make it so we only vary it by a small amount. So instead of trying to adjust a 0-5 volt range with a potentiometer, we can design the divider so we only have to adjust a 0 – 0.1V range with the same pot. Much easier!

vdiv

Say Vin is 12V, R1 is 10k and R2 is 820Ω. Vout is then 0.91V. If we insert a 1k potentiometer between R1 & R2,that means that the output can now vary about that point.

divider

So, how does this work? Let’s assume the pot is all the way in one direction, the voltage divider is then 1,820÷11,820 x 12 = 1.85V

with the pot all the way in the other direction, the output is 820÷11,820 x 12V = 0.83V

Nice! So now our trimpot only has to control a span of about 1V instead of a span of 12V. With high-resolution A/D converters and digital inputs, these basic techniques aren’t  used a lot these days, but they are still useful to have in your toolbox.

Now go design something!

Sensors for counting objects

In order to count anything, we need to detect it first. This usually means some kind of sensor. The sensor used will typically provide a signal that our counter can read. Most such sensors actually function as a type of switch because their output terminals are closing a circuit on the counter electronics that causes a count to increment.

The simplest sensor used to count objects is an actual physical switch. Microswitches are switches with very sensitive contacts: a light touch is all it takes to register the presence of an object. Often microswitches are made with levers to reduce the force needed or to have a greater reach.

lever-switch

 

One common application for this type of switch is in coin counters for arcade games. The coin falls through a slot,  tripping the lever as it rolls past the switch. The main advantage of microswitches is their low cost and reliability. A disadvantage of this type of counting sensor is that physical contact with the switch is required and the force required to trip the sensor can affect the object you’re counting.

Another common sensor type used as input to counters or object detectors is a photoelectric switch. This optical sensor detects the interruption of a beam of light, often invisible infrared light. For example, to count boxes on a conveyor belt, an emitter, typically an infrared LED shines a focused beam of light across the belt. When the beam is reflected by an object passing by on the belt, the detector sees the returned light and closes a circuit and this sends a pulse to the counter module, updating the count of items going by.

photo

Optical sensors have the advantage of not requiring contact with the switch, but may not work well in dirty or dusty environments where the optical signal may be blocked. Also, this type of sensor used for counting reflective items can be “fooled” by multiple reflections, causing an inaccurate count. In this case, a through-beam sensor, where the item must pass between the LED emitter and its detector, is often more reliable.

Magnetic sensors, as their name claims, detect magnetic fields. They are very useful when a non-contact sensor is needed in a dirty environment where light may be blocked.

 

Now that we’ve got sensors to detect the items, our PRT232 counter module is the ideal interface to do the actual counting. We can make modifications to the basic counter, such as a display, or special RS232 signal outputs,

Arduino Programming: Cycle timer

Sometimes you want an operation to repeat periodically. Say you are building a parts washer that circulates cleaning fluid around the dirty parts. The cleaning cycle might run for an hour and in that time you want the circulation pump to run for 10 seconds, stop for 5 seconds for particles to settle, then run for 10 seconds and repeat for an hour.

We need a timer. The type of timer that does this is called a Cycle Timer because it repeats a specific timing cycle and it’s pretty easy to build a cycle timer with an Arduino and a little bit of software programming. We’ll need an Arduino (any kind, from any manufacturer will work), a power supply, the power driver circuit, and the “load” which in this case is our pump.

Let’s get started.

// Which pin to use to control the load const int OUTPUT_PIN = 1; 
// Total number of cycles 
const int NUMBER_OF_CYCLES = 10; 
// On time per cycle in milliseconds 
const int CYCLE_TIME_ON = 500; 
// Off time per cycle in milliseconds 
const int CYCLE_TIME_OFF = 200; 

void setup() 
{
 pinMode(OUTPUT_PIN, OUTPUT);
 digitalWrite(OUTPUT_PIN, LOW);
} 

// Run the timer 
void loop() 
{
 int cycles = NUMBER_OF_CYCLES;
 while(cycles-- > 0)
 {
    // Turned timed output on
   digitalWrite(OUTPUT_PIN, HIGH);
   delay(CYCLE_TIME_ON);
   // Turn timed output off
   digitalWrite(OUTPUT_PIN, LOW);
   delay(CYCLE_TIME_OFF);
 }
 // Hold forever
 while(1);
}

Measuring water flow

A flow meter is the sensor that is used to measure water flow, or the flow of a low-viscosity fluid. There are many different types of flowmeters, but perhaps the most common are turbine or paddlewheel types. In these types of flow meters, a blade spins from the force of the moving fluid. The rotation is detected by a sensor that generates pulses that can be counted by a reader interface.

Older flow meters such as fuel pulsers used a rotating magnet that pulled a tiny reed switch causing the switch contacts to close. Every contact closure results in a pulse at the input of the reader, leading to these flowmeters being called pulsers. There are still thousands of these devices in use.

More modern pulsers and flow meters of all types like the one shown below generate their pulses electronically, often using Hall-Effect sensors that, again, respond to a moving magnet that is spun by a turbine or a paddlewheel.

flow

Now that we have a flowmeter device that can give a pulse output rate that is proportional to the rate that the water or other liquid is flowing at, we need to measure it. The reading device is basically a counter that is calibrated to the pulse rate.

For example, a fuel flowmeter may output 10 pulses per gallon of fuel dispensed, or a flowmeter used for water provides 100 pulses per liter. The reader must understand this calibration so it can display the correct value.

In many cases, the need is to read the flow and record or process the data on a desktop computer. Serial interfaces, RS232 or RS485 and USB are common here. By using a serial port flow meter interface, getting the data into the PC for software processing is a simple task since all modern programming platforms provide some form of serial data communication. Once the data can be received by your software, then you may record it, create graphs, log flow over time, etc.

Cedar Lake Instruments’s PRT232 flow meter interface is a serial port counter flowmeter reader that can measure and record flow. It reads pulser type flow meters, and can switch solenoid valves or pumps to control fluid flow.

Arduino Programming: Turn water on with Arduino and solenoid valve

Arduinos are popular small microcontroller boards that have many applications. However, they’re not designed to switch loads above a few milliamps: say a couple LEDs or so. While power-driver shields do provide this capability, they also can consume more resources than you may be able to give up.

We developed a high current driver to make it easy to control a solenoid valves with Arduino. It will also control pumps and motors. With an adapter cable, it can easily connect to your Arduino, BeagleBone, Raspberry Pi or other digital controller without soldering or crimping any connections. Doesn’t get any easier than that.

PwrDrvr1

The power driver board was born out of a need for controlling a 1 amp solenoid valve using an Arduino.  The solenoid valve was being used to control the water flow to fill a tank automatically. Now there’s a simple way to use your Arduino or compatible to switch up to 3A at 24VDC. Two output connections (the white wires shown above) connect directly the load (your solenoid, relay, motor, etc) and the power (red, black) go to the power supply (5 -24 volts). The orange lead is used to switch on and off. This is a low-voltage (5V) control that can connect directly to a microcontroller, or development board. An onboard LED indicates when the load is switched on.

Here’s some sample code that implements a timer with an Arduino. When the pushbutton is pressed, it turns on water flow for 3 seconds

// This sketch demonstrates a simple timer
// A load (motor, solenoid, relay, solenoid valve is on Pin 1
// A pushbutton to trigger the timer start is on pin 2
//
// When the pushbutton is held down for more than 0.1 second 
// then released, the timer starts
// and times out after 3 seconds
//
// Timer is retriggerable: if pushbutton pressed 
// during the timeout period, timer restarts
//
// Constant definitions
#define LOOP_INTERVAL 10
#define TIMEOUT 300 * LOOP_INTERVAL
#define TRIGGER_INTERVALS 10
#define TIMER_INACTIVE -1
#define TRIGGER_PIN 0
#define OUTPUT_PIN 1

void setup()
{
  pinMode(OUTPUT_PIN, OUTPUT);
  pinMode(TRIGGER_PIN, INPUT_PULLUP);
}

void loop()
{
  static int count = 0;
  static int timer = TIMER_INACTIVE;
  // Process loop periodically
  delay(LOOP_INTERVAL);
  
  // Check trigger input
  if (digitalRead(TRIGGER_PIN) == LOW)
  {
    // Must hold down pushbutton for the entire interval and 
    // then release to trigger
    count++;
  }
  else
  {
    // push button released. Check if we should start timing
    if (count >= TRIGGER_INTERVALS)
    {
      // Turn output ON (timeout is retriggerable)
      digitalWrite(OUTPUT_PIN, HIGH);
      timer = TIMEOUT;
    }
    count = 0;
  }
  
  // If timer active, count down
  if (timer != TIMER_INACTIVE)
  {
    timer -= LOOP_INTERVAL;
    if (timer == 0)
    {
      // Turn output OFF
      digitalWrite(OUTPUT_PIN, LOW);
      timer = TIMER_INACTIVE;
    }
  }
}      

Let’s find out what new applications you can come up with.

Power Driver ($11.95 shipping included)




Reading temperature on BeagleBone with AD592

A very common task is to measure temperature at various points. In fact, temperature is the most commonly measured and controlled variable in Process Control. The BeagleBone single-board computer is becoming more popular because of its low cost and sophisticated capabilities. It’s one of the easiest ways to serve up a web page that allows the operator to monitor data and control devices.

Temperature can be measured with a variety of sensors. Here we are concerned with the range of liquids from around 0C to 80C. A straightforward way to measure temperatures in this range is with a semiconductor analog-output sensor. They are easy to use, accurate, and low cost.

I happened to have a number of AD592 temperature to current sensors on hand. These have been traditionally used for process control because their current output makes them very noise resistant when long cable are used. We convert the current (1 microvolt/Kelvin) to a voltage by using a resistor. In this case, we use a 3.3kohm resistor to produce a voltage of around 1V at room temperature. The 3.3k resistor gives a resolution of .003V/Kelvin which with the 273.15K offset gives 0.003V/degree Celsius.

Using the sensor connected to 5V, AGND and the analog input on P9, pin 36, we use this code snippet to read the sensor:

// Read an AD592 temp sensor and return degF
function readTemp()
{
   var volts = 1.8 * trap.analogRead("P9_36");
   // Convert to Kelvin. AD592 outputs 1uV/K
   var k = volts / 3300 / 0.000001;
   // Convert to Fahrenheit
   var f= (k - 273.15) * 9 / 5 + 32;
   // Log the measured values
   console.log("V,k,F: " + volts,k,f);
   return f;
}

For improved accuracy we can measure the actual resistance of the 3.3k resistor and use that measured value in the program.

This concept is easily extended to remote reading. Since the BeagleBone is a powerful little Linux computer, it can be used to serve web pages. We have a simple node.js webserver and sensor data reader program available at this download link

Arduino Programming: hydraulic shock control from pushbutton

This was a response to an online request for help with code. A single  pushbutton cycles through three hydraulic damper valve settings. The code was too long to post on the site, so I’m showing it here.

As of today, I don’t know if it will work, but it explains the concept. I will get it working as time permits

// Digital output definitions
#define RED_OUTPUT 0
#define BLUE_OUTPUT 1
#define GREEN_OUTPUT 2

// Digital input definitions
#define SWITCH_INPUT 3

// Define states
#define RED 800
#define BLUE 801
#define GREEN 802

// Define message types
#define NONE 901
#define CLICK 902
#define PRESS 903
   
// Global state
int _state = GREEN;

// One-time call to initialize system
void setup()
{
   pinMode(SWITCH_INPUT, INPUT_PULLUP);
   pinMode(RED_OUTPUT, OUTPUT);
   pinMode(BLUE_OUTPUT, OUTPUT);
   pinMode(GREEN_OUTPUT, OUTPUT);
   // Enable GREEN state
   digitalWrite(GREEN_OUTPUT, HIGH);
}

// Continuously called by Arduino runtime
void loop()
{
   // Read pushbutton
   int message = readSwitch();
   // Update LEDs and hydraulic valve if state changed
   if (processState(message))
   {
      activateOutput(_state);
   }
}

// Read pushbutton and return the result
int readSwitch()
{
   const int CLICK_UPPER_LIMIT = 200;
   const int CLICK_LOWER_LIMIT = 25;
   const int PRESS_LOWER_LIMIT = 1000;
   const int PRESS_UPPER_LIMIT = 2000;

   int switchMessage = NONE;
   unsigned long start = micros();
   while (digitalRead(SWITCH_INPUT) == LOW);
   unsigned long duration = micros() - start;
   if (duration < CLICK_UPPER_LIMIT && duration > CLICK_LOWER_LIMIT)
   {
      switchMessage = CLICK;
   }
   else if (duration > PRESS_LOWER_LIMIT && duration < PRESS_UPPER_LIMIT)
   {
      switchMessage = PRESS;
   }
   return switchMessage;
}

bool processState(int message)
{
   static int lastState = GREEN;
   int retVal = 0;

   // PRESS overrides CLICK in all states
   if (message == PRESS)
   {
      _state = GREEN;
   }
   else
   {
      // Process states
      switch (_state)
      {
         case RED:
            if (message == CLICK)
            {
               _state = BLUE;
            }
         break;
            
         case BLUE:
            if (message == CLICK)
            {
               _state = RED;
            }
         break;
            
         case GREEN:
            if (message == CLICK)
            {
               _state = RED;
            }
         break;
            
         default:
         break;
      }
   }
   // Did state change?
   retVal = (lastState == _state);
   // Remember this state
   lastState = _state;
   // Return state changed information
   return retVal;
}

void activateOutput(int thisState)
{
   // Since this is only called on state change, OK to turn everything off first
   digitalWrite(RED_OUTPUT, LOW);
   digitalWrite(GREEN_OUTPUT, LOW);
   digitalWrite(BLUE_OUTPUT, LOW);
   // Turn on only the output corresponding to the state
   switch (thisState)
   {
       case RED:
         digitalWrite(RED_OUTPUT, HIGH);
         break;         
       case BLUE:
         digitalWrite(BLUE_OUTPUT, HIGH);
         break;      
       case GREEN:
         digitalWrite(GREEN_OUTPUT, HIGH);
         break;
      default:
         break;
   }   
}

Control a small DC pump

In an earlier post I showed how to wire a small DC pump to a power supply and a switch to turn it on and off. Even with simple manual control, there are still things to be aware of. Let’s say that you are building a small aquaponic or hydroponic system and you need a pump to circulate nutrient fluid to the plant roots. This is a situation where the pump only needs simple on/off control and can be left to run continuously.

There is a danger that over time the liquid will evaporate and its level will drop too low. Many liquid pumps cannot safely be run dry. This is because they are designed to use the liquid flowing through to cool them to a normal operating temperature. Without a continuous fluid flow, the motor will overheat.

This means we have to be aware of the liquid level. A float switch level sensor can be used in the control circuit to make sure that the pump does not run when the liquid is below a certain level. Most liquid level switches can’t control the current needed to run a pump so we use the switch to control a relay that has this needed current capacity.

It’s also important to remember that some pumps also have a maximum on-time rating. The pump may be able to run only (e.g.,) one minute continuously before it needs to be turned off to cool for three minutes. This proportional on/off time is referred to as its duty cycle.