Gesture Controlled Light Switch with Bluetooth Light Bulb

Objective

The objective of this project is to demonstrate gesture control by leveraging two 8-bit PIC devices connected through Bluetooth® to remotely operate a light bulb.

 Demo

Hardware Tools

Bulb

Switch

Software Tools

Tool About Installers
Installation
Instructions
Windows Linux Mac OSX
MPLAB® X
Integrated Development Environment
swtool-28px.png
MPLAB® Code Configurator
Dynamic Code Generation
MPLAB® XC8
C Compiler

Exercise Files

File Download
Installation
Instructions
Windows Linux Mac OSX
Project and Source Files

Overview

The system consists of two components: the light switch and the light bulb. The switch is made up of the gesture sensor, a PIC microcontroller, an RN4870 module, an LCD, and a buzzer. The light bulb has an LED driver, a PIC MCU, and an RN4870 module. The gesture sensor is used to control the bulb over Bluetooth with the LCD and buzzer being used for feedback.

A basic system diagram is shown below:

SystemDiagram.png

Technical Background

Gesture Detection

Gesture detection is used to determine what motion was performed over the sensor, to determine input control. The detection process uses IR emitters and directional sensors to determine the direction of the motion performed.

Detection Operation

The IR LED is powered, so that a train of pulses is emitted. These are reflected towards the sensors when something is within range. The proximity of the object can be determined with this information. Once an object is within the detection range, the gesture calculation mode will be entered. The detection of motion happens at different times based on each sensor and the way that they are lensed. The sensor corresponding to the direction of motion will pick up the motion first with the opposite picking it up last.

DetectionOperation.png

Once the gesture has been determined, it will be added to the gesture FIFO buffer. The gestures can be retrieved from the FIFO via I²C.

APDS9960

The APDS9960 is an integrated gesture, proximity, and color sensor. All these features are used in this system. Below is the state machine that the device runs through to alternate the operations.

StateMachine.png

Color Sensing

Color sensing works by having multiple photodiodes that have been filtered to be able to detect a certain portion of the color spectrum far better than the others. The proportion of the colors detected is calculated to determine the hue of the color perceived.

Bluetooth

Bluetooth is a wireless protocol that operates in the 2.4 GHz band and is characterized by its low power operation, small network operations, piconets, and Adaptive Frequency Hopping.

RN4870Click.jpg

Low-power Operation

Bluetooth can be very low power because of the way that transmissions are performed. The radio component of the Bluetooth transceiver can be configured to transmit the packets being sent and immediately go back to sleep. This aspect makes Bluetooth very appealing for battery-powered operations.

Network Operation - Piconets

Bluetooth networks operate using a master-slave configuration, where one master can send to and receive from many slave devices. This project does not leverage this feature since there are only two components. Each device has a unique ID that is used as an identifier on the network. It is a 12 digit hexadecimal value called a MAC address.

LED Driver

There are many ways to drive an LED but the device used in this case controls the devices using an I²C communication protocol. In this fashion, it is simple to control the intensity of the LEDs by sending simple I²C commands, and the device handles all the biasing of the LEDs based on local registers.

LEDDriverClick.jpg

Buzzer

The buzzer operates using a Pulse Width Modulation (PWM) signal. The buzzer must be operated at the frequency of the desired output. The volume is then controlled by the duty cycle of the PWM signal. The closer to 50% being the highest volume.

BuzzClick.jpg

System Program Overview

There are two halves to the system: the light bulb and the light switch. These two communicate using Bluetooth.

Light Bulb

The peripherals on this half of the system include the Bluetooth transceiver and the LED driver. The Bluetooth device is controlled over the Universal Asynchronous Receiver Transmitter (UART) and the LED driver is controlled using I²C.

Once powered, the PIC sends commands over I²C to the LED driver, setting it to a known light intensity value. It then waits for commands to be received over the Bluetooth connection. Once a command is received, it decodes the command and performs the required actions over I²C.

LightBulbDiagram.png

Light Switch

The peripherals on this half of the system are the Bluetooth transceiver, the gesture sensor, the LCD, and the buzzer. These are controlled using UART, I²C, Serial Peripheral Interface (SPI), and PWM respectively. Once powered, the PIC puts the Bluetooth device into command mode and sends the command to connect to the light bulb. Once it is connected, the device will initialize the gesture sensor and LCD, and then wait for a gesture to be detected. Once a gesture is detected, the PIC checks what mode it is in and performs the operations according to its state machine. If the operation requires changing the light, then the command is sent over UART. The modes can be changed using gestures as well. When each operation is performed, there is feedback in the form of the LCD changing and the buzzer beeping.

LightSwitchDiagram.png

Light Bulb Code

System Initialization

Power in the devices and set a default setting for the LED driver.

    __delay_ms(2000);       //Startup delay
    LED3_Initialize();      //Setup LED Click
    BLE_RST_SetHigh();      //Enable Bluetooth after LED is ready

Main Loop

Wait for the command to be received.

    while (1)
    {
        LED3_RecChange();   //Receive any Requests to change LED
    }

Decoding

Depending on the received command (in the form of a single ASCII character) perform the appropriate action. The different commands are displayed below.

Commands.png

Light Switch Code

Initialization

Initialize the LCD and gesture sensor, then power the Bluetooth device and request it to connect to the light bulb. Continue only when connected.

    BLE_MODE_SetLow(); // enter command mode   
    LCD_WriteString("BLE NOT CON!");
    __delay_ms(200);
    printf("C\r"); //tell bluetooth device to connect
    while(BLE_ST1_GetValue())   //Wait until bluetooth is connected
    CLRWDT();
    __delay_ms(150);
    BLE_MODE_SetHigh(); // Exit bluetooth command mode

Main Loop

The main loop is made up of three components.

Gesture Check

When the gesture flag is set, the PIC enters the decoding portion of the loop to determine the action to take. This is determined by the last gesture performed and the current mode flag.

if(gesture && !color)           // process new gesture
        {
            gesture--;
            TMR1_Reload();
            uint8_t dir = gestureProcess();
            switch(mode)
{…….

Processing

Once the action is determined, it is executed.

Processing.png

Timeout Check

If the Timeout check flag is set, and the mode isn’t on the default, then change back to default mode. This is the way to move backward through modes.

if(timeout)     //Timeout routine
    {
     timeout = false; // clear flag
     CLRWDT();
     if(mode != 0)        //return to default mode
    {
    PWM5_Pulse100();
    mode = 0;        //CHANGE MODE DEFAULT
    ………..

Interrupts

Gesture

Though not used, the framework to make use of interrupts for the gesture sensor is implemented.

Timeout

When the TMR1 timer expires every five seconds, the interrupt routine sets the global timeout flag. This will be acted upon in the main loop.

Libraries

The MPLAB® Code Configurator (MCC) was used to generate I²C, SPI, UART, and PWM control functions. The Sparkfun Arduino library was ported to control the gesture sensor.

© 2020 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.