Bluetooth-Controlled AC Light Dimmer with Android Mobile App

Overview

The light level in a room has a huge effect on one’s mood and productivity. Lighting can contribute to your alertness and awakeness in the mornings, and it can either entice you to feel tired or keep you up all night. With all of these discoveries on the effects of lighting, it is becoming more and more important to be able to control lighting. In addition, a mobile app that uses Bluetooth to control and dim lights would be perfect!

Having a dimmer installed in the lighting systems of your home or office gives you the ability to adjust the intensity and brightness based upon the particular time of day and the specific task at hand.


Hardware Used

      • Arduino Uno R3 – 1
    
    
  • Ac Light Dimmer – 1
  • 
    
  • HC-05 Bluetooth Module – 1
  • 
    
  • LED lamp – 1
  • 
    
  • Breadboard – 1
  • 
    
  • Jumper Wires
  • 
    

    Important note: Luminescent lamps or gas discharge lamps do not support dimming. Filament lamps or LED lamps are recommended, but not all support dimming either. Make sure to check if the lamp is capable of dimming effects.

    Software Used

    Libraries Used


    Application Description

    AC Light Dimmer Module

    The AC Light Dimmer is designed to control the alternating current voltage, which can transfer current up to 600V/16А. It is basically a TRIAC with some supporting components. The power part of the dimmer is isolated from the control to exclude the possibility of high current disruption to a microcontroller. This is mainly used to smoothly change the light brightness of lamps, but can also be applied to control fans, pumps, air cleaners, etc.

    How does it work?

    The AC Light Dimmer uses Pulse Skipping Modulation (PSM), or as some sources call it, Pulse Skip Modulation, to achieve dimming. It has a zero-crossing detection, which gives a signal on the Z-C pin every time the AC sine wave crosses zero voltage. PSM works with modulating the number of pulses or full sine wave periods that are transmitted to the load. Imagine you want to dim the light in 100 distinct values. For 100% brightness, PWM pin should be set to HIGH when a full sine wave is starting up until 100 periods. For 25% brightness, PWM pin should be set to HIGH for 25 full periods and then LOW for the next 75 periods. To make things easier, all these will be handled by the Robotdyn dimmer library which will be discussed later down in the codes.

    Specifications:

    • Power: 600 V / 16 A
    • AC Frequency: 50/60 Hz
    • TRIAC: BTA16 – 600B
    • Modulation: Pulse Skipping Modulation
    • Logic Level Input Voltage: 5 V

    HC-05 Bluetooth Module

    The HC-05 Bluetooth Module is an easy to use Bluetooth SPP (Serial Port Protocol) module, designed for transparent wireless serial connection setup. It is one of the highest rated and used wireless communication protocols in embedded projects due to its low cost and wide range of applications.

    How does it work?

    Serial port Bluetooth module is fully qualified Bluetooth V2.0+EDR (Enhanced Data Rate) 3Mbps Modulation with complete 2.4GHz radio transceiver and baseband. It uses CSR Bluecore 04-External single chip Bluetooth system with CMOS technology and with Adaptive Frequency Hopping feature. In layman’s terms, the Bluetooth technology manages the wireless communication channel. The Bluetooth modules can transmit and receive the data wirelessly. It communicates via serial communication which makes an easy way to interface with a microcontroller.

    Specifications:

    • Input Voltage: 5 V
    • Communication Method: Serial
    • Frequency: 2.4 GHz
    • Default PIN/password: 1234
    • Master and slave mode can be switched

    Hardware Setup

    HC-05 Bluetooth module connections

    • RXD connected to Arduino pin D5
    • TXD connected to Arduino pin D4
    • Vcc and Gnd connected to 5V and Ground respectively

    AC Light Dimmer module connections

    • Z-C connected to Arduino pin D2
    • PWM connected to Arduino pin D3
    • Vcc and Gnd connected to 5V and Ground respectively

    Android App Setup

    For the Android mobile app, we will use the cloud-based MIT app inventor. In just a few steps, you can set it up and start building apps! First things first, you will need to install the App Inventor Companion App on your Android device. If you do not have an Android device, you’ll need to install software on your computer so that you can use the on-screen Android emulator. Learn more about the initial setup by clicking here.

    Once all that is done, click on the Project tab, then start a new project and give it a proper name. When you’re in the main interface, you’ll see a palette on the left, a viewer in the middle, then a components and properties tab on the right. Drag and drop the following components from the palette onto the viewer screen accordingly and in order (left). To make it easier to identify the components, change the label names to whatever you want using the rename button that is below (right). Following this format with specific components inside a certain arrangement makes it easier to hide or show a group of components at a time when needed later on.

    Change the label and button texts accordingly and add an image to the BluetoothBTN to give the user a button to click on for connecting. You can also add a background image on Screen1 to make it look prettier (left). After this, hide VerticalArrangement3 so it will look like a pre-connected screen (right).

    To see how the app looks on your phone, connect it to the App Inventor Companion by clicking on the connect button on the top menu and selecting AI companion. An importable .aia file download for this will be included below with the code, as well as an .apk for direct installation.


    Code

    Arduino Code

    #include <SoftwareSerial.h>
    #include <RBDdimmer.h>
    
    #define pwm  3
    #define zerocross  2 // for boards with CHANGEBLE input pins
    
    SoftwareSerial BTSerial(4, 5); // RX, TX
    //dimmerLamp dimmer(pwm, zerocross); //initialize port for dimmer for ESP8266, ESP32, Arduino due boards
    dimmerLamp dimmer(pwm); //initialize port for dimmer for MEGA, Leonardo, UNO, Arduino M0, Arduino Zero
    
    char input; //a string to hold incoming data
    int minBright = 35; //35 and 85 for the yellow bulb
    int maxBright = 85;
    bool stringComplete = false;
    
    void setup() {
      Serial.begin(9600);
      BTSerial.begin(9600);
      dimmer.begin(NORMAL_MODE, ON); //dimmer initialization: name.begin(MODE, STATE) 
      Serial.println("AC Light Dimmer program has started.");
    }
    
    void loop() {
      serialEvent();
      if (stringComplete) {
        int fadeValue = map(input, 0, 100, minBright, maxBright);
        dimmer.setPower(fadeValue);
        Serial.println(fadeValue);
        stringComplete = false;
      }
    }
    
    void serialEvent() {
      while (BTSerial.available()) {
        input = BTSerial.read();
        stringComplete = true;
      }
    }

    MIT App Inventor Code


    Code Breakdown

    Arduino Code

    Pre-Initialization

    #include <SoftwareSerial.h>
    #include <RBDdimmer.h>
    
    SoftwareSerial BTSerial(4, 5); // RX, TX

    Before we start, we must include the libraries to be used in the project. The Arduino hardware has built-in support for serial communication on pins 0 and 1. The SoftwareSerial library allows serial communication on other digital pins of the Arduino which allows us to use pins 4 and 5 as a receiver and transmitter respectively. This also allows us to use it as a debug serial and print out the message received later on.

    #define pwm  3
    #define zerocross  2 // for boards with CHANGEBLE input pins
    
    //dimmerLamp dimmer(pwm, zerocross); //initialize port for dimmer for ESP8266, ESP32, Arduino due boards
    dimmerLamp dimmer(pwm); //initialize port for dimmer for MEGA, Leonardo, UNO, Arduino M0, Arduino Zero
    
    char input; //a string to hold incoming data
    int minBright = 35; //35 and 85 for the yellow bulb
    int maxBright = 85;
    bool stringComplete = false;

    Next, we define the pins to be used for PWM and zero-crossing. In our case, PWM is in pin 3. If you’re using an Arduino Uno, Leonardo, Mega, M0, or Zero, you don’t need to define the zero-crossing pin since it is already set by default in the Robotdyn library. In case you’re using other boards, define the zero-crossing pin accordingly and use the commented out dimmerLamp line of code to initialize the dimmer instead.

    After that, we initialize the variable for an input and set the minimum and maximum brightness. The values for these will depend on the lamp used. However, several tests have to be conducted to get them. In my case, anything on or below 35 won’t turn on the lamp so I set that as the minimum and anything above or on 85 won’t make it brighter so I set that as the maximum.

    void setup()

    Serial.begin(9600);
      BTSerial.begin(9600);
      dimmer.begin(NORMAL_MODE, ON); //dimmer initialization: name.begin(MODE, STATE) 
      Serial.println("AC Light Dimmer program has started.");

    Inside the setup() function, we set the serial and Bluetooth serial baud rate to 9600 bps which is the default for serial communication. We then initialize the dimmer and print a message in the Serial to indicate a successful initialization.

    serialEvent function

    void serialEvent() {
      while (BTSerial.available()) {
        input = BTSerial.read();
        stringComplete = true;
      }
    }

    Before going to the loop() function, we must first understand what role the serialEvent function play, serialEvent checks for when there is data in the Bluetooth Serial. While there’s data available, it will store that data in the input variable and then set the boolean stringComplete to true to indicate that it has received the data successfully.

    void loop()

    serialEvent();
      if (stringComplete) {
        int fadeValue = map(input, 0, 100, minBright, maxBright);
        dimmer.setPower(fadeValue);
        Serial.println(fadeValue);
        stringComplete = false;

    Inside the loop() function, we call in the serialEvent which will tell us when it has received data successfully. If it has received data, it goes into the if statement and maps the input value from 0 to 100 to the minimum and maximum brightness we have set earlier. After that we set the lamp’s power with dimmer.setPower() then print the value on the serial for debugging purposes. We then set the boolean stringComplete back to false to indicate that it has finished receiving the data and there are no more available to receive.

    MIT App Inventor Code

    Connection buttons

    Since the MIT App Inventor uses code blocks, it should be easier to understand. When the BluetoothBTN (ListPicker) is clicked, “.BeforePicking” is what happens before the user picks something from a certain list. In this case, we want to show the user a list of Bluetooth devices nearby that they can connect to so we set the BluetoothBTN’s elements to the that.

    “.AfterPicking” is what happens after the user has picked a Bluetooth device to connect to. If it has connected to the HC-05 Bluetooth module successfully, it will set the visibility of VerticalArrangement2, which contains the ConnectLabel and BluetoothBTN to false and set visibility of VerticalArrangement3, which contains the other components to true. The app should look like this afterwards.

    The DisconnectBTN does exactly what it names implies – it disconnects the app from the Bluetooth device and sets VerticalArrangement2 back to true and VerticalArrangement3 back to false, which are the settings prior to connection.

    Slider

    Here’s the most important part of the code. “.PositionChanged” is triggered when the slider’s position changes. Inside the function we set the BrightnessValue label’s text to the slider’s thumb position. The thumb position is where the slider is currently at so having the label indicate its value will be helpful for the user. At the same time, it checks if the user is still connected to the Bluetooth device and sends the thumb position value via Bluetooth for the HC-05 Bluetooth module to receive.


    Conclusion

    Bluetooth wireless communication has been rising in popularity nowadays due to its low cost and wide range of applications. Moreover, most modern phones have Bluetooth, making it excellent controllers for Bluetooth-supported devices. This opens up a lot of possibilities with IOT that incorporate Bluetooth as a way of communication.

    MIT App Inventor is a great place for beginners and hobbyists to begin making their mobile apps. It does not require a lot of background in programming and has an interface that is easy to use and understand. In other words, it’s a great stepping stone towards creating mobile applications.


    References

    The post Bluetooth-Controlled AC Light Dimmer with Android Mobile App appeared first on CreateLabz.

    Ac light dimmerAndroidArduinoBluetoothDimmerHc-05Hc05KnowledgebaseLightMit app inventorMobile appTx-rx

    Leave a comment

    All comments are moderated before being published