Get Adobe Flash player
FacebookTwitterGoogle+
English Arabic French German Italian Portuguese Russian Spanish

Did you know?

An electric oven uses one kilowatt-hour of electricity in about 20 minutes, but one kilowatt-hour will power a TV for 3 hours, run a 100-watt bulb for 12 hours, and keep an electric clock ticking for 3 months.
 

Help us stay online:

small donate

btledctrl logoIt is time for fun. If you accepted that using Android phone to control hardware is fun:) Android become very popular and if you into microcontrollers it is pity not to use that knowledge for making cool applications. If you experienced with microcontrollers it is useful to expend your knowledge with any object oriented language. That can be useful for future job opportunities. For Android you need to know Java programming and usage of Eclipse IDE for Java developers. Maybe sounds hard, but today there are a lot of free online courses about Android and plenty of examples and complete projects.

For this project we make new hardware. Microntroller dsPIC30F4013 was used and Microelektronika microBUS is implemented. USB power supply makes device easy to use. Note: On schematic as voltage regulator TS2940 is used, not BA33BC0FP. Our goal is to make new projects using Mikroelektronika click boards. This is our first project with this hardware. Complete schematics is given at picture, an if you want to make your own board just contact us.

dsPICClick-page-001

Android application description. We wanted to use Bluetooth click and Android phone to control LEDs tied on dsPIC microcontroller. As inspiration we examine project at Karel Blogs and we want to gratitude for sharing this excellent project. Remember that we are not Android experts but with this project we managed to make things works (hooray). Adopted android application when receive any character from dsPIC (via Bluetooth), sends array of character according to ImageButton states. Application has four ImageButton for different color of LED (see picture) and "connect" button for making Bluetooth connection. When connect button is pressed, new activity (view) appeared with saved and available Bluetooth devices (2nd view on picture). If Bluetooth on device is off, dialog will appear to ask you to turn it on. Than you can choose device and make connection. If you use Mikroelektronika Bluetooth Click module, you have to enter default passkey (1234).

screen shot btledctrl

If you accepted that Karel's code works fine (as we did), we only need to change main class and main xml file. Here is code for BluetoothLEDControl class

/*
 * Copyright 2011 Karel Bruneel (www.karelbruneel.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package electronics.base.com.btledctrl;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
public class BluetoothLEDControl extends Activity implements
		BluetoothSPPConnectionListener {
	private static final int REQUEST_CONNECT_DEVICE = 1;
	private static final int REQUEST_ENABLE_BT = 2;
	private BluetoothSPPConnection mBluetoothSPPConnection;
	private BluetoothAdapter mBluetoothAdapter = null;
	private boolean connected = false;
	private byte gravity[];
	private ImageButton imageButton1, imageButton2, imageButton3, imageButton4;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_led_panel);
		// Initializing the gravity vector to zero.
		gravity = new byte[4];
		gravity[0] = 'a';
		gravity[1] = 'c';
		gravity[2] = 'e';
		gravity[3] = 'g';
		imageButton1 = (ImageButton) findViewById(R.id.imageButton1);
		imageButton1.setImageResource(R.drawable.led_red_gray);
		imageButton1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (gravity[0] == 'a') {
					gravity[0] = 'b';
					imageButton1.setImageResource(R.drawable.led_red);
				} else {
					imageButton1.setImageResource(R.drawable.led_red_gray);
					gravity[0] = 'a';
				}
			}
		});
		imageButton2 = (ImageButton) findViewById(R.id.imageButton2);
		imageButton2.setImageResource(R.drawable.led_green_gray);
		imageButton2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (gravity[1] == 'c') {
					gravity[1] = 'd';
					imageButton2.setImageResource(R.drawable.led_green);
				} else {
					gravity[1] = 'c';
					imageButton2.setImageResource(R.drawable.led_green_gray);
				}
			}
		});
		imageButton3 = (ImageButton) findViewById(R.id.imageButton3);
		imageButton3.setImageResource(R.drawable.led_blue_gray);
		imageButton3.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (gravity[2] == 'e') {
					gravity[2] = 'f';
					imageButton3.setImageResource(R.drawable.led_blue);
				} else {
					gravity[2] = 'e';
					imageButton3.setImageResource(R.drawable.led_blue_gray);
				}
			}
		});
		imageButton4 = (ImageButton) findViewById(R.id.imageButton4);
		imageButton4.setImageResource(R.drawable.led_white_gray);
		imageButton4.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (gravity[3] == 'g') {
					gravity[3] = 'h';
					imageButton4.setImageResource(R.drawable.led_white);
				} else {
					gravity[3] = 'g';
					imageButton4.setImageResource(R.drawable.led_white_gray);
				}
			}
		});
		mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		// If bluetooth is not activated, ask the user to activate
		if (!mBluetoothAdapter.isEnabled()) {
			Intent enableBtIntent = new Intent(
					BluetoothAdapter.ACTION_REQUEST_ENABLE);
			startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
		}
		// Initializing the bluetooth SPP connection.
		// Register this as the BluetoothSPPConnectionListener.
		mBluetoothSPPConnection = new BluetoothSPPConnection(this); // Registers
																	// the
		// Initializing the "connect" button.
		// Register this an the OnClickListener.
		Button bt = (Button) findViewById(R.id.connect);
		bt.setText("Connect");
		bt.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// When not connected, start the MainActivity that allows
				// the user to select a device.
				// The activity will return a bluetoothdevice and on that
				// return, the onActivityResult() (see below)
				// member function is called.
				if (!connected) {
					Intent serverIntent = new Intent(v.getContext(),
							MainActivity.class);
					startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
					// When connected, close the bluetooth connection.
				} else {
					mBluetoothSPPConnection.close();
				}
			}
		});
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		// Close the bluetooth connection
		mBluetoothSPPConnection.close();
	}
	@Override
	protected void onPause() {
		super.onPause();
	}
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case REQUEST_CONNECT_DEVICE:
			if (resultCode == Activity.RESULT_OK) {
				String address = data.getExtras().getString(
						MainActivity.EXTRA_DEVICE_ADDRESS);
				BluetoothDevice device = mBluetoothAdapter
						.getRemoteDevice(address);
				mBluetoothSPPConnection.open(device);
			}
			break;
		case REQUEST_ENABLE_BT:
			if (resultCode == Activity.RESULT_OK) {
			}
			break;
		}
	}
	public void bluetoothWrite(int bytes, byte[] buffer) {
		byte[] command = new byte[5];
		command[0] = 'y';
		command[1] = gravity[0];
		command[2] = gravity[1];
		command[3] = gravity[2];
		command[4] = gravity[3];
		mBluetoothSPPConnection.write(command);
	}
	public void onConnecting() {
		// This function is called on the moment the phone starts making a
		// connecting with the bluetooth module.
		// The function is executed in the main thread.
		// Change the text in the connectionInfo TextView
		TextView connectionView = (TextView) findViewById(R.id.connectionInfo);
		connectionView.setText("Connecting...");
	}
	public void onConnected() {
		// This function is called on the moment a connection is realized
		// between the phone and the bluetooth module.
		// The function is executed in the main thread.
		connected = true;
		// Change the text in the connectionInfo TextView
		TextView connectionView = (TextView) findViewById(R.id.connectionInfo);
		connectionView.setText("Connected to "
				+ mBluetoothSPPConnection.getDeviceName());
		// Change the text in the connect button.
		Button bt = (Button) findViewById(R.id.connect);
		bt.setText("Disconnect");
		// Send the 's' character so that the communication can start.
		byte[] command = new byte[1];
		command[0] = 's';
		mBluetoothSPPConnection.write(command);
	}
	public void onConnectionFailed() {
		// This function is called when the intended connection could not be
		// realized.
		// The function is executed in the main thread.
		connected = false;
		// Change the text in the connectionInfo TextView
		TextView connectionView = (TextView) findViewById(R.id.connectionInfo);
		connectionView.setText("Connection failed!");
		// Change the text in the connect button.
		Button bt = (Button) findViewById(R.id.connect);
		bt.setText("Connect");
	}
	public void onConnectionLost() {
		// This function is called when the intended connection could not be
		// realized.
		// The function is executed in the main thread.
		connected = false;
		// Change the text in the connectionInfo TextView
		TextView connectionView = (TextView) findViewById(R.id.connectionInfo);
		connectionView.setText("Not Connected!");
		// Change the text in the connect button.
		Button bt = (Button) findViewById(R.id.connect);
		bt.setText("Connect");
	}
}

Logic is quite simple, we use byte array to store ASCI characters and ImageButtons to change those characters. For example, character 'a' means that red LED is off and 'b' means LED is on (lines 51 and 55). You are right, not briliant logic but very educational :). You can notice that we change pictures of buttons for visual interpretation. "Off" picture is gray-scaled. Rest of code is kind a copy of original project. You can download complete BluetoothLEDControl project to learn more about Android programming. Our test application is available HERE.

Hardware realization. Schematic of hardware is given above. First we need to setup Bluetooth module. After reading datasheet of RN41 Bluetooth module, we realized that GPIO7 and GPIO3 must be shortened as it shown on picture.

bluetooth click

GPIO7 when high force baud rate of 9600, GPIO3 when high autodiscovery option is enabled. UART2 of dsPIC is used for communication with Bluetooth module. In infinite loop, character 'a' is send to Android to trigger sending array of character from Android phone. Download complete MPLAB project, here is main.c code:

#include<p30fxxxx.h>
#include <string.h>
#include <delay.h>
#include <libpic30.h>
#include "uart.h"
#include "timer.h"
_FOSC(CSW_FSCM_OFF & FRC_PLL4); //7.37 MHz Internal RC oscillator, 4x PLL enabled
_FWDT(WDT_OFF);                 //Watchdog disabled
#define DELAY_RANDOM 3000000
#define RXbuffLen 16
int tempRX, tempRX2;
char buf[10];
char uartRXbuffer[RXbuffLen];
int tempUART;
char received;
unsigned int stopwatch;
//-----------------------------------------------------------------
void __attribute__((__interrupt__)) _U1RXInterrupt(void)
{
    IFS0bits.U1RXIF = 0;
    tempRX = U1RXREG;
}
void __attribute__((__interrupt__)) _U2RXInterrupt(void) {
    IFS1bits.U2RXIF = 0;
    tempRX2 = U2RXREG;
    for (tempUART = 0; tempUART < RXbuffLen; tempUART++) {
        uartRXbuffer[tempUART] = uartRXbuffer[tempUART + 1];
    }
    uartRXbuffer[RXbuffLen - 1] = tempRX2; //put character in array (backward)
    if (tempRX2 != '0') {
        received = 1;      //flag in on - something was received
    }
}
void __attribute__((__interrupt__)) _T1Interrupt(void) // TIMER INTERRUPT
//-----------------------------------------------------------------
{
    TMR1 = 0;               //Stop Timer
    stopwatch++;            //for Delay_ms() function
    IFS0bits.T1IF = 0;      //Clear timer flag
}
//-----------------------------------------------------------------
//-----------------------------------------------------------------
void Delay_ms(unsigned int time)
//-----------------------------------------------------------------
// funkcija za kasnjenje u ms
{
    stopwatch = 0;
    while(stopwatch < time);
}
void ClearBuffer() {
    unsigned char priv;
    for (priv = 0; priv < RXbuffLen; priv++) {
        uartRXbuffer[priv] = '0';
    }
}
int main(void)
{
    InitUART1();
    InitUART2();
    Init_T1();
    received=0;
    ClearBuffer();
    ADPCFGbits.PCFG10=1; //Pin RB10 digital
    TRISBbits.TRISB10=0;// PB10 output
    LATBbits.LATB10 = 1;
    ADPCFGbits.PCFG0=1; // Pin RB0 digital
    TRISBbits.TRISB0=0; // PB0 output
    LATBbits.LATB0 = 1;
    ADPCFGbits.PCFG2=1; // Pin RB2 digital
    TRISBbits.TRISB2=0; // PB2 output
    LATBbits.LATB2 = 1;
    ADPCFGbits.PCFG4=1; // Pin RB4 digital
    TRISBbits.TRISB4=0; // PB4 output
    LATBbits.LATB4 = 1;
    WriteStringUART1("Start");
    WriteUART1(13);
    while (1)
    {
        WriteStringUART1("Sending to module\r");
        WriteUART2(97); //Send to Bluetooth 'a' character to trigger sending from Android phone
        if(received == 1)
        {
            //WriteStringUART1("Received:  ");
            received = 0; //reset flag
            //WriteStringUART1(uartRXbuffer); //UART buffer
            //WriteUART1(13);
            //WriteUART1(uartRXbuffer[13]);   //Test what is on 13th position in buffer
            //WriteUART1(13);
            if(uartRXbuffer[12] == 'b')
            {
                LATBbits.LATB2 = 0;    //red LED on
            }
            if(uartRXbuffer[12] == 'a')
            {
                LATBbits.LATB2 = 1;    //red LED off
            }
            if(uartRXbuffer[13] == 'd')
            {
                LATBbits.LATB4 = 0;
            }
            if(uartRXbuffer[13] == 'c')
            {
                LATBbits.LATB4 = 1;
            }
            if(uartRXbuffer[14] == 'f')
            {
                LATBbits.LATB0 = 0;
            }
            if(uartRXbuffer[14] == 'e')
            {
                LATBbits.LATB0 = 1;
            }
            if(uartRXbuffer[15] == 'h')
            {
                LATBbits.LATB10 = 0;
            }
            if(uartRXbuffer[15] == 'g')
            {
                LATBbits.LATB10 = 1;
            }
            ClearBuffer();
        }
        Delay_ms(500); //for faster response you can decrease delay
    }//while
    return 0;
}//main

Here are picture and video of complete hardware:

hw bluetooth