View on GitHub

AutomatedHomeCurtains

BLE curtain control with BluChip by Makerchips

Imagine waking up and wanting to get a beam of sunshine through your windows, or shutting the curtains so you can sleep in further, without the effort of getting yourself up close to the curtains but rather with the touch of a button on your smartphone. With the Automated Home Curtain System, you get to achieve this with components that cost no more than $90!

The Design

At the heart of the Automated Home Curtain System is MakerChips' BluChip module.

Figure 1: BluChip Explorer Kit product image from MakerChips' website.

The BluChip is a tiny 16.6x11.15mm Bluetooth module that can serve as a peripheral to smartphones via BTLE.

Click here for an introduction to Bluetooth Low Energy(BTLE).

The module consists of an nRF51 SoC by Nordic Semiconductors, a great platform for BLE applications as it supports many integrated features on both Android and Apple apps.

BluChip Explorer Kit

Figure 2: BluChip Explorer Kit & ARM Programmer by MakerChips.

To build this project, I got the BluChip Explorer Kit from MakerChips which arrived in 2 separate boxes, one for the CMSIS-DAP programmer and another box containing the BluChip on a breadboard with 2 RGB LEDs, a photo-resistor and a CR2032 battery.

Figure 3: CMSIS-DAP Programmer(top) & BluChip with 2 LEDs, a photo-resistor and CR2032 battery(bottom)

As you've noticed, the BluChip module is extremely tiny, making it perfect for small embedded low powered Bluetooth projects. It fits on a footprint of only 6x4 0.1" headers on a breadboard and has additional 0.05" headers on the top of the board, pretty impressive for a commercially FCC certified package!

Figure 4: BluChip v1 module top view(top) and bottom view(bottom).

Here are some key features of the BluChip from MakerChips' website:

nRF Connect App

As soon as you pop open the BluChip explorer box, you see it come to life with blinking LEDs, quite a fascinating sight, isn't it?

Video 1: BluChip Explorer Kit unboxed.

To see what's in store with this BLE module, let's go ahead and install the nRF Connect app from Google Play or App Store.

Figure 5: Installation of nRF Connect from the Google Play store.

We are going to connect to the BluChip with our phone, so open the nRF Connect app, browse through the welcome screen, and tap Enable to turn Bluetooth on. Next, tap Scan and you'll soon discover that your BluChip device is listed under the Scanner tab.

Figure 6: nRF Connect app opened, and discovered devices listed under the Scanner tab.

Before we actually connect to the BluChip, let's get an LED and place it on the breadboard next to pins 026(+ve) and 021(-ve). The LED should immediately light up because pin 026 outputs 3.3V (logic level HIGH) whereas pin 021 is logic LOW(Ground).

Figure 7: LED lights up when pins inserted onto breadboard [positive(+) on pin 026; negative(-) on pin 021].

Go ahead and tap connect to establish a connection between your smartphone and the BluChip, which then takes you to the device's client tab in the app.

Figure 8: BluChip device client tab on the nRF Connect app.

The BluChip client tab displays all the services available on your device. What we're interested here is the BlueChip GPIO Service (listed as Unknown Service). Tap on it and then tap on the upward facing arrow next to the GPIO Modulation Characteristic (listed as Unknown Characteristic).

Figure 9: GPIO Service(listed as Unknown Service with UUID e3c90001-13d7-5395-7843-b9da2c4307ef) and GPIO Modulation Characteristic(listed as Unkown Characteristic with UUID e3c90002-13d7-5395-7843-b9da2c4307ef) under the BluChip tab.

A write value popup will surface, giving you the option to send data to your BluChip device. In our case, we want to turn the LED off, so tap on the arrow next to BYTE ARRAY and change the data format to UINT 8. We will be sending the pin number as the first value, so enter 21 for pin021. Tap on add value to send the next piece of data, the state of which the pin is to be set (hex BYTE format). To turn the LED off, we will be setting pin 021 to 3.3V(logic level high), so enter 01 then tap Send.

Figure 10: Sending values to the BluChip device through GPIO Modulation Characteristic on nRF Connect app.

The LED instantaneously turns off! To turn the LED back on, send a value of 0x00(logic level LOW) to pin021. As seen below the listed characteristic, the sent value of (0x) 15-01 is displayed. {[(decimal UINT8) 21 = (hex BYTE) 0x15] + (hex BYTE) 0x01 => (hex BYTEs) 0x1501 }

Figure 11: LED on pins 026 & 021 turns off after pin 021 driven to 3.3V(logic HIGH) from nRF Connect BLE app(top). Sent value seen below Characteristic in nRF Connect app(bottom).

If you choose save these values on the Write value popup by giving it a name and then tapping save, you can load them in the future as presets for easy GPIO modulation!

Video 2: Toggling an LED on the BluChip by loading and sending saved presets with GPIO Service through Modulation Characteristic on the nRF Connect app.

Programming the BluChip

You would have noticed from the video above that the BluChip device name on my phone is different from yours, so how do we go about changing it to our very own liking?

The application firmware running on the BluChip serves as a Peripheral device(slave) over BLE to Central devices(master) such as smartphones connected to it. To change our device's name, let's dig in to flashing application firmware onto our BluChip.

Included with the BluChip Explorer kit is the ARM Programmer(CMSIS-DAP). MakerChips has provided a neat How-To guide on the details of flashing firmware onto the BluChip with the CMSIS-DAP.

To compile firmware into a hex file and flash it, we will need Keil, nRF51 Software Development Kit(SDK), and BluChip firmware. Go ahead and download them from the links in "The Software" section on MakerChips' Programming the BluChip with CMSIS-DAP and Keil page.

Figure 12: Download Keil, SDK v10.0 & BluChip firmware from links in "The Software" section on MakerChips' Programming the BluChip with CMSIS-DAP and Keil page.

Install Keil, then follow steps 1-3 in the "Creating the Hex File" section.

Figure 13: Follow steps 1-3 in "Creating the Hex File" section on MakerChips' Programming the BluChip with CMSIS-DAP and Keil page.

At this point, you may continue to Step 4, Rebuilding all Target Files.

If you get an error regarding "core_cm0.h", you would need to add its path to the project to compile it.

Figure 14: Build error: cannot open "core_cm0.h" indicates a missing path entry.

We simply would need to search for the file, and locate its directory, which is "\components\toolchain\gcc".

Figure 15: core_cm0.h located in "\components\toolchain\gcc".

Let's include this path to our project. Click Options for Target, go to the C/C++ tab then include the path as shown below.

Figure 16:Click Options for target. Go to the C/C++ tab. Click … next to Include paths. Add "..\..\..\..\..\..\components\toolchain\gcc" by clicking the box to the left of the red arrow, followed by clicking ok.

After including necessary dependencies, our project compiles and we can now view the compiled output, a custom hex file at "nRF51_SDK_10.0.0_dc26b5e\examples\ble_peripheral\ble_app_ahc-master\bluchip\s110_with_dfu\arm4\_buildnrf51422_xxac_s110.hex".

Figure 17 : Follow steps 1-8 in "Transferring the Hex File" section on MakerChips' Programming the BluChip with CMSIS-DAP and Keil page.

To flash the hex file onto the BluChip, follow steps 1-8 in the "Transferring the Hex File" section.

Figure 18 : Click Download (F8) and Keil will indicate the time at which the Flash Load completes.

Now that you have loaded firmware onto the BluChip with a custom Device name, fire up the nRF Connect app and scan for your device. You will notice that it's now named after what you've define in the DEVICE_NAME in the firmware!

Figure 19 : BluChip changes to name defined in DEVICE_NAME as seen in nRF Connect app.

In the next section, we will begin to set up the hardware, electronics & software of our Automated Home Curtain System.

Building the Automated Curtains

After having reviewed the process of compiling and flashing our firmware, let's move on to building our very own bluetooth curtains!

A stepper motor will be used to drive a timing belt which moves the curtains open and close. The stepper motor is driven by a Half-H driver IC that will be controlled by the BluChip.

For power, we will use a 12V AC-DC voltage regulator that is fed to the motor, together with a LM317 DC-DC voltage regulator to step down 12V to 3.3V which will power the BluChip and Stepper Driver IC.

You can get your very own BluChip module from MakerChips' brand new store over at Tindie, or from the MakerChips Website.

Let's get the parts listed below in addition to the BluChip Explorer Kit to begin assembling the automatic curtains:

12V 1A Power Adapter $3.40

Barrel Jack $0.68

LM317T Voltage Regulator $0.80

Resistors(200 & 330 Ohm) $1.69

L293D Stepper Driver $1.63

Unipolar Stepper Motor $8.00 (or $1.66 <= modify this smaller unipolar into a bipolar stepper)

6mm Timing Belt $7.31

6mm Gear $0.54 (or 3D printable from Thingiverse)

6mm Pulley $1.17 (or 3D printable from Thingiverse)

Limit Switch x2 (optional) $1.34

Project Enclosure Box (optional) $1.06

Breadboard Jumper Wires $2.09

Dupont Jumper Wires $2.80

Rubber Bands $1.13

Twist Ties $3.22

22 AWG Wire (optional) $1.22

Zip ties (optional) $0.63

Shrink tube (optional) $1.97

Tools (optional):

Hot Glue Gun $3.75

Solder Iron $6.79

Download Bill of Materials from GitHub(Amazon)

Figure 20: Automated Home Curtain Breadboard View.

The diagram above shows how you are going to wire up the system, depending on what features you choose to add. If you want more precise movement, you would add limit switches to the project.

Limit switches are endpoints to the curtains which tell the BluChip when it is opened or closed. Without the Limit Switches, you would need to configure the firmware to indicate how far your curtains move in the upcoming "Firmware Configuration" section.

The diagram above also includes an optional photo resistor which allows for day and night detection, also configurable in the "Firmware Configuration" section.

Start the assembly of the hardware off by mounting the stepper motor, pulley & timing belt to the top of your curtains.

Figure 21: Top to bottom: Gear fixed onto motor shaft; Motor mounted on left side of curtain rail; Pulley mounted on right side of curtain rail; Belt looped around gear and pulley parallel to curtain rail, tensioned with rubber bands.

Temporarily tension the timing belt with rubber band. Later on, before completing the project, you are going to zip tie it together to hold it permanently.

To attach the curtains to your timing belt, loop Wire Ties around the belt and curtain hook.

To get a better idea on how to hook up the curtains to the belt, follow the diagram below. You will be tying the left curtain to the rear of the timing belt with a wire tie, and the right curtain to the front of the timing belt with a wire tie.

Figure 22: Top view of hardware assembly: left curtain is wire tied(orange) to the rear of the timing belt. Right curtain is wire tied to the front of the timing belt. When motor turns counterclockwise, the wire ties pull both curtains open.

Once you get the belt secured and curtain tied, remove the stepper motor so that we can begin to assemble and test the electronic circuit that will drive it.

Start building the electronics by placing the Bluchip, L293d IC and LM317t Voltage Regulator on the breadboard according to Figure 20.

Figure 23: LM317t Voltage Regulator, L293d INC & BluChip inserted into the breadboard according to Figure 20.

Insert the 200 & 330 ohm resistors according to Figure 20.

Figure 24 : 200 & 330 Ohm resistors inserted onto the breadboard according to Figure 20.

The resistors adjust the LM317's output so that it provides ~3.3V.

Figure 25: LM317t TO-220 Pinout.

Insert the jumper wire as seen below , then a wired barrel jack which is shown below.

Figure 26:Jumper wire & barrel jack inserted onto breadboard(top). Barrel jack wired with positive(red) terminal at the back and Ground(black) terminal in the center.

Let's plug in our power adapter to the wall socket, and plug the adapter into the barrel jack to test the voltages as seen in Figure 27.

Figure 27 : Probe negative on the bottom left strip of the breadboard (Ground Rail) and positive on the Input pin of LM317t to read ~12V(top). Probe negative on Ground and Positive to the Output pin of LM317t to read ~3.3V(bottom).

Once correct voltages have been ascertained, remove the power jack and start placing the remaining breadboard jumper wires according to Figure 20.

Figure 28 : Jumper wires placed onto breadboard according to Figure 20.

Next up, we are going to wire up our bipolar stepper motor to the L293d IC.

First, place Dupont jumper wires into the stepper motor connector as seen below.

Figure 29: Dupoint jumper wires inserted into bipolar stepper motor connector.

To know which wire goes where, follow the schematic below.

Figure 30: Automated Home Curtain System Schematic.

As seen in the schematic above, the leads from one coil goes to Pin2 & Pin6 of the L293D. Leads from the other coil goes to Pin11 & Pin14.

The modified 28BYJ-48 bipolar stepper motor has four usable colored wires as seen below.

Figure 31 : Pink and orange wire connected to coil 1, yellow and blue to coil 2.

We wire blue to Pin3, yellow to Pin6, orange to Pin11 and pink to Pin14 on the L293d.

The basic circuitry is now complete!

If you want to implement limit switches, wire the NO & C leads to some 22AWG wire. At the other end, attach DuPont jumpers to form leads that fit onto the breadboard.

Figure 32 : First limit switch wire(~1 feet) is connected to the C(common) & NO(normally open) pins of the limit switch. The second limit switch wire(~half the length of curtain rails) is wired the same.

You can mount them onto the curtain rail as shown below with rubber bands, or if you have a hot glue gun at hand you can zip tie it to the rail then dab a good amount of hot glue to ensure it doesn't move around.

Figure 33: Limit switch mounted to curtain rail with rubber band.

To get an idea of where to place them, refer to the diagram below.

One limit switch is attached to the far-left end of the curtain rail, between the first rail hook and the second, so that when the curtains open the hook presses against the switch and activates it. The other limit switch is placed directly at the center of the rail, facing left. This way, it gets activated when the curtains close.

Figure 34 : Opened limit switch(left) and Closed limit switch(center). They face each other.

Insert the limit switch leads onto the breadboard according to Figure 20.

Figure 35: One end of Opened limit switch attached to Pin 025 and the other to Ground. One end of Closed limit switched attached to Pin03 and the other to Ground according to Figure 20.

Finally, if you want your curtains to open when the sun rises and close when it sets, you would need to wire up the photo resistor as shown below, and set it up close to where it has access to sunlight during dawn.

Figure 36: Photoresistor wired and mounted facing outside(top & middle). Photoresistor wires inserted into Pin027 & Power Rail according to Figure 20(bottom).

After you're done with the breadboard circuit set up, get ready and connect your programmer to the BluChip to flash the firmware. Download the firmware from GitHub and extract it into your SDK directory as you did before.

Download ble_app_ahc.zip from Github

Open the project up, then compile and upload the firmware to the BluChip.

Before testing it out, we will enclose the breadboard in a box and make holes for the wires and our Curtain Status LED.

Place the breadboard onto the base of the enclosure box and make an opening for the wires. The opening also serves as a point for the BluChip to communicate to other devices through its antenna.

Figure 37 : An opening is made close to the BluChip for wires & antenna access.

Drill a hole the size of the LED on the side of the enclosure and mount the LED on it. Wire the LED according to Figure 20.

Figure 38: Top to bottom: Drill a hole; wire the LED, mount the LED with some hot glue; insert the leads onto the breadboard according to Figure 20; final enclosure ready to be mounted.

Find a suitable place to mount the enclosure box to the left of the curtain rail, close to a power outlet. Remount the motor and do a final tension test of the timing belt, making sure no slack is present.

Figure 39 : Automated Home Curtain System mounted onto Curtain Rail.

Now it's time to test our assembled system. Insert the power adapter and fire up your nRF Connect app. You will discover a device named Curtains.BluChip.

Connect to it, send a value of UINT8 1(Open curtains) to the Unknown Characteristic under Unknown service, and watch the curtains open!

Video 3: Value 0x01 sent to Curtains.BluChip through Curtain Characteristic from nRF Connect app.

Now that you have successfully tested your system, let's have a look at configuring some of the code that is running the show on the BluChip.

BluChip Firmware Configuration

The Automated Home Curtain firmware project consist mainly of 4 files: main.c, ahc.c, ble_ahc_service.c & ble_ahc_service.h.

While building the electronics & hardware, we had the option to choose if we wanted limit switches to increase the accuracy of our automated system.

In the code from ahc.h, we can see #define for LIMIT_SWITCHES.

Figure 40 : #define LIMIT_SWITCHES enables the use of limit switches in our project. #undef LDR disables the photoresistor option in our project.

Compiling and flashing code with #define LIMIT_SWITCHES enables the use of both limit switches to detect when the curtains have opened and closed.

Figure 41 : When LIMIT_SWITCHES are defined, they check the limit switch inputs to see if the curtains have reached the end of opening/closing.

Renaming it to #undef LIMIT_SWITCHES is needed if you opted not to include limit switches for your project. In this case, you would need to fine tune the distance to which your curtain travels in the variables CURTAIN_OPEN_STEPS and CURTAIN_CLOSE_STEPS. Adjust these values to lengthen or shorten the curtain travel distance.

Figure 42: CURTAIN_OPEN_STEPS & CURTAIN_CLOSE_STEPS can be adjusted when limit switches are disabled.

The other option, adding a photoresistor, can be enabled by modifying #undef LDR to #define LDR. LDR stands for Light-dependent resistor, also known as photoresistor. When we enable LDR, the photoresistor knows when its bright or dark outside, and helps you shut or open your curtains at the start or end of the day.

Figure 43 : By defining LDR, we enable the BluChip's Low Power Comparator. This causes the event function LP_COMP_IRQHandler to fire at dusk or dawn, causing the curtains to close and open respectively.

Besides configuring the Limit Switches and Photoresistor, let's take a look at some of the other main blocks of code that allow you to automatically open and close the curtains.

The files ble_ahc_service.c & ble_ahc_service.h contain code that transmits data from your phone to the BlueChip.

Figure 44 : ble_ahc_service.c & .h contains definitions of the BLE Services and Characteristics that we view on nRF Connect. This project contains the AHC Service, together with the Curtain Characteristic.

When the BluChip receives the data, it parses it according to if a 0 or a 1 is sent. It then activates the status LED, performs motor movement, and then deactivates the LED signaling completion.

Figure 45: Function curtain_write_handler parses the data from nRF Connect, deciding on whether to ignore an invalid message or open(1) and close(0) the curtains.

The function ahc_init() from ahc.h is run at the beginning of the main loop, initializing all pins on the BluChip.

Figure 46 : Function ahc_init() configures the onboard LED, Photoresistor, Curtain Status LED, Stepper Motor & Limit Switches.

Summary

To conclude, this was an extremely fun and fairly easy project to get to learn BLE basics. The fact that BluChip's breakout module fits snugly on a breadboard makes it really easy to prototype quickly on any breadboard that you might have laying around.

I would say that after building my automated curtains, I've already thought of various other things to hook the BluChip up with, including smart neopixels, an OLED to create a digital watch, a smartphone controlled robot, and many other low powered electronic project ideas that would need compact wireless communication!

Anyone with a keen interest in electronics and programming would be pleasantly surprised at what the BluChip has to offer, as well as the convenience of setting up and implementing BLE to turn projects into even cooler ones.

As of now, I'll get back to enjoying my handy automated Home Curtains:

Video 4: Using the Automated Home Curtains with the nRF Connect App.

Automated Home Curtains

See this tutorial at Instructables