Introduction: Bluetooth Controlled LED With Analogue LDR Input for TfCD

Paul Bosch - 4154746
Duygu Guroglu - 4519191

This project is a variation on the RFduino Test and Android Sample App from Iann (Iann RFDuino Test App)

We rewrote the existing test program for the RFduino and the test application for Android in order to get to the desired result. Our aim was to send an analogue input signal via bluetooth to the controller that controls the actuator also via bluetooth (see the block diagram below). The old sample used a digital button input and the application provided a separate function to turn on or off the LED.

With the help of the RFduino module, we managed to get an analogue LDR input and send it via Bluetooth to a smartphone application. In the same application, this input was rerouted as output and send via bluetooth to control an LED connected with the RFduino (see block diagram).

Step 1: REQUIRED ELEMENTS

Required Hardware:

- RFD22102 RFduino DIP

- RFD22121 USB Shield

- LED

- LDR

- 10K ohm resistor

- 330 ohm resistor

- Wire set

- Breadboard

Required Software:

- Arduino

- RFduino Drivers (in case not installed automatically):

- RFduino library (Install in Arduino by going to Sketch->include library->Add .ZIP library and select the downloaded library)

- Application builder (Android Studio was used here)

- RFduino Test App created by Iann (Android v5.1 used)

Step 2: ​PREPARATION

Download and install the programs to the pc.

Connect the RFDuino to the pc.

Step 3: SCHEMATIC OF THE CIRCUIT

With the help of Fritzing Schematic of the required circuit is visualized.

Build the circuit and connect it to the RFDuino.

The LED is put in series with the 330 ohm resistor and connected to the ground. The input and voltage comes from the GPIO 3 pin.

The LDR is put in series with the 10k ohm resistor and connected to the ground. The input voltage comes from the 3.3V pin and the GPIO 5 pin is connected between the LDR and resistor.

Step 4: CODES FOR ARDUINO

/*

Based on a code from OpenSourceRF.com.

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */

#include <RFduinoBLE.h>

int led = 3; // GPIO 3 is used here

int sensor = 5; // GPIO 5 is used here

int threshold = 900; // Check serial monitor for the correct threshold

void setup() {

Serial.begin(9600);

pinMode(led, OUTPUT);

pinMode(sensor, INPUT);

// this is the data we want to appear in the advertisement

// (if the deviceName and advertisementData are too long to fix into the 31 byte

// ble advertisement packet, then the advertisementData is truncated first down to

// a single byte, then it will truncate the deviceName)

RFduinoBLE.advertisementData = "ledbtn";

// start advertising so our smartphone can scan and find the device

RFduinoBLE.begin();

}

void loop() {

// read the value of the LDR

int sensorValue = analogRead(sensor);

// The LDR will send data to the smartphone Application

// If the measured value is lower than the threshold, the Arduino will send a "1" to the App

// The App will then send a "1" to the RFduino causing the LED to light up

if (sensorValue > threshold) {

RFduinoBLE.send(0);

delay(1000);

} else {

RFduinoBLE.send(1);

delay(1000);

}

Serial.println(sensorValue);

delay(10);

}

void RFduinoBLE_onDisconnect() {

// don't leave the led on if they disconnect

digitalWrite(led, LOW);

}

void RFduinoBLE_onReceive(char *data, int len) {

// if the first byte is 0x01 / on / true

if (data[0])

digitalWrite(led, HIGH);

else

digitalWrite(led, LOW);

}

Step 5: CODES FOR APPLICATION

The APK file is added here. You can simply download it on your Android device and try it.

Or change the original file by Iann (http://forum.rfduino.com/index.php?topic=44.0) manually. In that case:

Look in the MainActivity.java for the following piece of code and add the line that is made bold:

private void addData(byte[] data) {

View view = getLayoutInflater().inflate(android.R.layout.simple_list_item_2, dataLayout, false);

TextView text1 = (TextView) view.findViewById(android.R.id.text1); text1.setText(HexAsciiHelper.bytesToHex(data));

rfduinoService.send(data);

String ascii = HexAsciiHelper.bytesToAsciiMaybe(data);

if (ascii != null) {

TextView text2 = (TextView) view.findViewById(android.R.id.text2);

text2.setText(ascii);

}

dataLayout.addView(

view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

}

Step 6: FINAL PROTOTYPE

The video shows the final working prototype.

Of course, since we are not programmers, there is a lot to improve. This instructable only shows the very first basics on how to manipulate the existing software to use the bluetooth input as bluetooth output.

Some suggestions for improvement

- Instead of creating a new text box for every input (the list becomes very long as you can see in the video), the input could be displayed in just one text box where the number would change instead of adding a new number to it.

- The old buttons from the applications could be removed.

- New feedback on the on/off state of the LED could be given.

Possible application

We used BLE to activate or deactivate a light when it is connected to our Android application. However, this is just a basic application but has great potential to become something of interest. For example, this method could be be used to create a remote controller to change the home lighting environment. With a low light intensity of the environment, the light will be activated by itself as soon as it connects to the application via Bluetooth. Entering and leaving a room could then automatically turn on or off the lights, saving energy.