Control Your Arduino Board Using an iOS Device with the BLExAR App

If you want to control your Arduino board with the BLExAR app, the code and wiring can be found below. The wiring is for an HM-10 (CC254x) board, and the demonstration later in the tutorial uses an RGB LED (wired separately). Also, a kit has been assembled that includes these three components and is linked to the right.

NOTE: Be sure to upload the control code below to an Arduino board PRIOR to wiring the CC254x BLE module to the board. The user can also change the pin designation ble_device(A,B) to any pins on the Arduino (A = 3, B = 4; for example), then the user can upload code while it is wired.

BLExAR RGB LED Dimmer Bundle BLExAR RGB LED Dimmer Bundle
Sold out
Quick View
BLExAR RGB LED Dimmer Bundle
$20.00

This bundle includes the necessary components to follow along with the RGB LED fade tutorial using the BLExAR app. The user will wire, program, and demonstrate how to change the color of a tri-color LED with the BLExAR app using both PWM control and on-off switches on the app. The PWM control of the RGB LED lets users select from a range of colors using a combination brightness of the three colors available on the LED.

This kit comes with the following components:

  • 1x Maker Portal Arduino Uno Board

  • 1x Black USB 2.0 Cable (1m)

  • 1x CC2541 Bluetooth Module

  • 1x RGB LED (with 3 anode legs, 1 cathode leg)

  • 1x Mini Breadboard

  • 8x Male-to-Male Jumper Wires

*iOS device NOT included!

NOTE: Our batch of RGB LED modules have reversed labels for green and red, meaning, the label R is for green, and G is for red. This was noticed after receiving and testing the modules from our manufacturer.

#include <SoftwareSerial.h>

String strglob= "";
String pin_num_char = "";
int pin_num = 255;
bool analog_bool = false;
bool digital_bool = false;

SoftwareSerial ble_device(0,1);

// the setup routine runs once when you press reset:
void setup() {
  Serial.begin(9600);
  delay(100);
  ble_device.begin(9600);
}

void loop() {

  while (ble_device.available()){
    char ble_char = ble_device.read();    
    if (ble_char == '\n'){
      Serial.println(strglob);
      if (analog_bool){
        pinMode(pin_num,OUTPUT);
        analogWrite(pin_num,strglob.toFloat());
        analog_bool = false;
        pin_num = 255;
      } else if (digital_bool){
        pinMode(pin_num,OUTPUT);
        if (strglob == "1"){
          digitalWrite(pin_num,HIGH);
        } else if (strglob == "0"){
          digitalWrite(pin_num,LOW);
          pinMode(pin_num,INPUT);
        }
        digital_bool = false;
        pin_num = 255;
      }
      pin_num = 255;
      strglob = "";
    } else {
      if (analog_bool == false and digital_bool == false) {
        if (ble_char == 'D'){
          digital_bool = true;
        } else if (ble_char == 'A'){
          analog_bool = true;
        } else {
        }
      } else{
        if (pin_num == 255){
          if (ble_char != 'e'){
            pin_num_char += ble_char;
          } else if (ble_char == 'e'){
            pin_num = (String(pin_num_char).toInt());
            pin_num_char = "";
          }
        } else {
          strglob += ble_char;
        }
      }
    }
  }
  strglob = "";
  delay(10);
}
BLExArduino_wiring_digram.png

Wiring for Arduino Control using Bluetooth and iOS

From here, your Arduino board is controllable using the BLExAR app and your iOS device. Wire an LED or low-power fan to one of the pins and control the component using your iPhone or iPad!

 

See More in BLExAR and Arduino: