Project tutorial
[App Inventor IoT] LED fading (PWM)

[App Inventor IoT] LED fading (PWM) © GPL3+

Control LED intensity (PWM) with a slider in your Android app.

  • 1,962 views
  • 3 comments
  • 3 respects

Components and supplies

About this project

Introduction

this project will show you how to use App Inventor of BLE (Bluetooth Low Energy) to interact with the Arduino 101 development board in various ways. You must import BLE component(.aix) as an extension before using it. A screenshot of the actual app execution is shown below. 

YouTube 影片

Arduino 101 is the latest development board under cooperation between Arduino.cc (NOT .org) and Intel, also known as Genuino 101 outside of the USA.

App Inventor

Basically, this example is almost identical with [App Inventor and Arduino: Lesson 1: LED Blink], except we use BLE component in this case. In this example we will use the slider to control the brightness of Arduino 101 D9 LED.

Designer Page

Here a Slider that is used to control the brightness of the LED, along with Clock components that regularly send the position of the slider pointer to Arduino 101 through BLE components. The position of the slider pointer is also displayed in the TextBox. You cannot use the original BluetoothClient components to communicate with Arduino because of the difference in the communication protocol. 

Blocks

1. Initialize and connect

In the Initializing window use BluetoothLE1 to scan for available devices, which will detect if your Arduino 101 is ready. 

Next, click the ConnectButton and in the ConnectButton.Click window choose BluetoothLE1 to connect to the specified device. Type in your Arduino 101 bluetooth address here (98:4F…). Please modify the address string according to your Arduino 101.

2. Connection confirmed

BluetoothLE1.connected window will be called after the connection is established, as shown below.

3. Continuous Transmission Signal

Use the Clock.Timer command to accomplish the following two tasks with a time interval of 100 (every 0.1 second)

·         Display the position of the pointer of the slider in the TextBox.

·         Use BluetoothLE1.WriteIntValue command to send out the pointer position, then with this information Arduino 101 will control the brightness of the LED via the analogWrite command. Type in “19B10011-E8F2-537E-4F6C-D104768A1214″ in both service_uuid and characteristic_uuid columns. Arduino 101 uses this parameter as its BLE service.

4. Disconnect

When you press the Button_Disconnect button (Button_Disconnect.Click window), BluetoothLE1 component will disconnect with the specified device (Arduino 101).

Arduino 101 Code

Copy the code and paste it onto Arduino 101. Please notice that Arduino 101 has onboard BLE hardware, therefore you don't need to connect other Bluetooth modules such as HC05.

service_uuid and characteristic_uuid are also specified in the sketch as 

19B10010-E8F2-537E-4F6C-D104768A1214″ and

19B10011-E8F2-537E-4F6C-D104768A1214″(one digit difference),  which must be the same with the string you use in App Inventor.

/*   
  A simple demo of how to control Arduino 101' LED fading with App Inventor's BLE component.
  By CAVEDU (service@cavedu.com)
*/
#include <CurieBLE.h>
BLEPeripheral blePeripheral;  // BLE Peripheral Device (the board you're programming)
BLEService lightService("19B10010-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service
// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEUnsignedIntCharacteristic switchCharacteristic("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
const int lightPin = 9; // pin to use for the Light
void setup() {
  Serial.begin(9600);
  // set Light pin to output mode
  pinMode(lightPin, OUTPUT);
  // set advertised local name and service UUID:
  blePeripheral.setLocalName("Light Service");
  blePeripheral.setAdvertisedServiceUuid(lightService.uuid());
  // add service and characteristic:
  blePeripheral.addAttribute(lightService);
  blePeripheral.addAttribute(switchCharacteristic);
  // set the initial value for the characeristic:
  switchCharacteristic.setValue(0);
  // begin advertising BLE Light service:
  blePeripheral.begin();
  Serial.println("BLE Light service.");
}
void loop() {
  // listen for BLE peripherals to connect:
  BLECentral central = blePeripheral.central();
  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's MAC address:
    Serial.println(central.address());
    // while the central is still connected to peripheral:
    while (central.connected()) {
      // if the remote device wrote to the characteristic,
      // use the value to control the Light:
      if (switchCharacteristic.written()) {
          Serial.println(switchCharacteristic.value());
          int light = map(switchCharacteristic.value(),10,49,0,255);
          analogWrite(lightPin,light);
          Serial.println(light);
      }
    }
    // when the central disconnects, print it out:
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }

Code

app inventor aiaJava
No preview (download only).
arduino 101 sketchArduino
/*
  A Simple example that alows you to turn on/off a light connected to a digital relay (switch)  using a Arduino 101 microcontroller.

  Author Andrew McKinney (mckinney@mit.edu)

*/
#include <CurieBLE.h>

BLEPeripheral blePeripheral;  // BLE Peripheral Device (the board you're programming)
BLEService lightService("19B10010-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service

// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEUnsignedIntCharacteristic switchCharacteristic("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);


const int lightPin = 9; // pin to use for the Light

void setup() {
  Serial.begin(9600);

  // set Light pin to output mode
  pinMode(lightPin, OUTPUT);

  // set advertised local name and service UUID:
  blePeripheral.setLocalName("Light Service");
  blePeripheral.setAdvertisedServiceUuid(lightService.uuid());

  // add service and characteristic:
  blePeripheral.addAttribute(lightService);
  blePeripheral.addAttribute(switchCharacteristic);

  // set the initial value for the characeristic:
  switchCharacteristic.setValue(0);

  // begin advertising BLE Light service:
  blePeripheral.begin();

  Serial.println("BLE Light service.");
}

void loop() {
  // listen for BLE peripherals to connect:
  BLECentral central = blePeripheral.central();
  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's MAC address:
    Serial.println(central.address());

    // while the central is still connected to peripheral:
    while (central.connected()) {
      // if the remote device wrote to the characteristic,
      // use the value to control the Light:
      if (switchCharacteristic.written()) {
          Serial.println(switchCharacteristic.value());
          int light = map(switchCharacteristic.value(),10,49,0,255);
          analogWrite(lightPin,light);
          Serial.println(light);
      }
    }

    // when the central disconnects, print it out:
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }
}

Schematics

arduion LED D9
arduino101_led_0awznRa5jN.fzz

Comments

Similar projects you might like

Control RGB LED by Dragging – Arduino 101 & App Inventor

Project tutorial by DFRobot and CAVEDU Education

  • 4,474 views
  • 0 comments
  • 13 respects

Open Data Home Automation - Arduino 101 & App Inventor

Project tutorial by 3 developers

  • 2,599 views
  • 1 comment
  • 11 respects

IoT Made Easy w/ UNO, ESP-01, ThingSpeak & MIT App Inventor

Project tutorial by MJRoBot

  • 17,879 views
  • 15 comments
  • 35 respects

Bluetooth control led with lcd led status display real time.

Project tutorial by Youssef Sabaa

  • 17,314 views
  • 4 comments
  • 34 respects

Smart Garbage Monitoring System Using Arduino 101

Project tutorial by Technovation

  • 22,689 views
  • 7 comments
  • 37 respects
Add projectSign up / Login