IoTaLabs
Published © MIT

Adding Bluetooth to Your Arduino (+Light Sensor)

A guide to adding and enabling Bluetooth to your Arduino project using Adafruit nrf8001.

IntermediateFull instructions provided7,593
Adding Bluetooth to Your Arduino (+Light Sensor)

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
3 mm LED: Green
3 mm LED: Green
×1
Resistor 1k ohm
Resistor 1k ohm
×1
Photo resistor
Photo resistor
×1
Resistor 33k ohm
×1
Adafruit - nRF8001 Bluetooth Module
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Adafruit nRF8001 Schematics

Breadboard Circuit Diagram

Code

Bluetooth LED Arduino Code

Arduino
/*********************************************************************
This is an example for our nRF8001 Bluetooth Low Energy Breakout

  Pick one up today in the adafruit shop!
  ------> http://www.adafruit.com/products/1697

Adafruit invests time and resources providing this open source code, 
please support Adafruit and open-source hardware by purchasing 
products from Adafruit!

Written by Kevin Townsend/KTOWN  for Adafruit Industries.
MIT license, check LICENSE for more information
All text above, and the splash screen below must be included in any redistribution
*********************************************************************/

// This version uses the internal data queing so you can treat it like Serial (kinda)!

#include <SPI.h>
#include "Adafruit_BLE_UART.h"

// Connect CLK/MISO/MOSI to hardware SPI
// e.g. On UNO & compatible: CLK = 13, MISO = 12, MOSI = 11
#define ADAFRUITBLE_REQ 10
#define ADAFRUITBLE_RDY 2     // This should be an interrupt pin, on Uno thats #2 or #3
#define ADAFRUITBLE_RST 9


Adafruit_BLE_UART BTLEserial = Adafruit_BLE_UART(ADAFRUITBLE_REQ, ADAFRUITBLE_RDY, ADAFRUITBLE_RST);

const int led = 3;
char val = 'x' ;
uint8_t sendbuffer[20];

/**************************************************************************/
/*!
    Configure the Arduino and start advertising with the radio
*/
/**************************************************************************/
void setup(void)
{ 
  BTLEserial.setDeviceName("LIGHT"); /* 7 characters max! */
  BTLEserial.begin();
  pinMode(led,OUTPUT);
  digitalWrite(led,LOW);
}

/**************************************************************************/
/*!
    Constantly checks for new events on the nRF8001
*/
/**************************************************************************/
aci_evt_opcode_t laststatus = ACI_EVT_DISCONNECTED;

void loop()
{
  delay(50); // slow down execution to 20 times per second
  
  // Tell the nRF8001 to do whatever it should be working on.
  BTLEserial.pollACI();

  // Ask what is our current status
  aci_evt_opcode_t status = BTLEserial.getState();

  if (status == ACI_EVT_CONNECTED) {
    // Lets see if there's any data for us!
    // OK while we still have something to read, get a character and print it out
    while (BTLEserial.available()) {
      val = BTLEserial.read();
      if (val == '1'){
        digitalWrite(led,HIGH);
        sendData("\nLED ON");
      }  
      
      if (val == '0'){
        digitalWrite(led,LOW);
        sendData("\nLED OFF");
      }  
    }
  }
}

void sendData(String Send)
{
  Send.getBytes(sendbuffer, 20);
  char sendbuffersize = min(20, Send.length());
  // write the data
  BTLEserial.write(sendbuffer, sendbuffersize);
}

Credits

IoTaLabs

IoTaLabs

1 project • 11 followers
We are a group of DIY enthusiasts that try to build cool stuff. Check out our latest project at doteverything.co

Comments