# A CircuitPython BLE Remote Control On/Off Switch

## Overview

https://youtu.be/1J2wCvvjqDI

I have multiple computers around the house, and sometimes they need to be rebooted remotely. For example, here’s a surveillance camera server that’s located in a locked closet.

![](https://cdn-learn.adafruit.com/assets/assets/000/082/342/medium800/light_CP_Solenoid01.jpg?1570991246)

Currently the only way to turn it on is by pressing the illuminated blue push button on the front of the case. Instead of running around the house, to manually reboot the computer, we will use CircuitPython to automate this process!

The goal of this tutorial is to build a BLE peripheral that attaches to the front so the server can be turned on remotely from outside the closet. The peripheral will also wirelessly report whether the server is on or off.

![](https://cdn-learn.adafruit.com/assets/assets/000/082/456/medium800/light_CP_Solenoid16.jpg?1571179105)

This CircuitPython tutorial demonstrates how to build a battery powered Bluetooth BLE remote control to operate a BLE peripheral hosting a solenoid and a spectrally selective light detector. Two Adafruit Feather nRF52840 Express boards will be used for the BLE client and server.

# A CircuitPython BLE Remote Control On/Off Switch

## Parts

### Part: Adafruit Feather nRF52840 Express 
quantity: 2
CircuitPython compatible Feather with Bluetooth Low Energy and native USB 
[Adafruit Feather nRF52840 Express ](https://www.adafruit.com/product/4062)

### Part: Mini Push-Pull Solenoid - 5V
quantity: 1
Very small 20mm long solenoid with a captive armature and a return spring
[Mini Push-Pull Solenoid - 5V](https://www.adafruit.com/product/2776)

### Part: N-channel power MOSFET - 30V / 60A
quantity: 1
IRLB8721 logic level MOSFET
[N-channel power MOSFET - 30V / 60A](https://www.adafruit.com/product/355)

### Part: 10K Ω Resistor
quantity: 1
Through-hole resistor 1/4 watt 5%
[10K Ω Resistor](https://www.adafruit.com/product/2784)

### Part: 1N4001 Diode
quantity: 1
Kickback protection diode
[1N4001 Diode](https://www.adafruit.com/product/755)

### Part: Lithium Ion Polymer Battery
quantity: 1
Ideal battery for Feathers - 3.7V 400mAh
[Lithium Ion Polymer Battery](https://www.adafruit.com/product/3898)

### Part: Switch
quantity: 1
Mini slide or rocker switch 
[Switch](https://www.adafruit.com/product/805)

### Part:  Green LED
quantity: 1
HLMP-CM1G-350DD 525nm 15° water clear lens
[ Green LED](https://www.mouser.com/ProductDetail/Broadcom-Avago/HLMP-CM1G-350DD?qs=%2Fha2pyFadujLZpv1YRzD1d2GEtLFgco%2FRr%2FNmTnhH0LCrEd%252BO%2FA1zg%3D%3D)

### Part: Light Tube
quantity: 1
2mm single strand end glow fiber optic filament
[Light Tube](https://thefiberopticstore.com/product/2-0mm-single-strand-filament/)

### Part: 15M Ω Resistor
quantity: 1
Through-hole resistor 1/4 watt 5%

### Part: 82nF Ceramic Capacitor
quantity: 1
Filtering capacitor 10V or higher

### Part: Steel Threaded Rods
quantity: 2
M5 x 250mm Fully Threaded

### Part: Steel Nuts
quantity: 4
M5 hex

### Part: Nylon Screw
quantity: 1
M3 x 8mm socket head

### Part: Nylon Spacer Standoff
quantity: 1
M3 x 6mm (cut to length)

# A CircuitPython BLE Remote Control On/Off Switch

## Light Sensing

There are several ways to determine if a device is powered on such as a non-contact current sensor or measuring voltage on the PCB, but I think the least expensive and least likely to void the warranty will be to detect if the blue light on the on/off push button is illuminated.

LED’s are great for emitting light, but they can also be used for detecting light. Many electronic devices can be reversed. For example a motor can be powered by electricity or it can generate electricity, a speaker can also be used as a microphone and an LED will generate current when exposed to light. Additionally, LED’s are spectrally selective. In other words, they can detect specific colors of light.

![](https://cdn-learn.adafruit.com/assets/assets/000/082/440/medium800/light_CP_Solenoid02.jpg?1571175089)

Generally speaking LED’s, are most sensitive to light that is lower than their dominant emission wavelength. Therefore, a yellow or red LED may yield the best results for detecting a green LED. Likewise an aqua or green LED could be used to detect blue light. My testing found that a sensing LED with a wavelength 50nm greater than the emitting LED worked best. If you don’t know the wavelength of the emitting LED and you don’t have an expensive spectrometer, it can easily be found using a diffraction grating such as this slide which cost under a dollar.

![](https://cdn-learn.adafruit.com/assets/assets/000/082/442/medium800/light_CP_Solenoid03.jpg?1571175340)

Place a ruler directly in front of the LED to measure. Then position the slide at a known distance of at least 5 feet from the LED. Longer distances tend to give more accurate results. Looking through the slide you’ll see that some of the light gets diffracted.

![](https://cdn-learn.adafruit.com/assets/assets/000/082/443/medium800/light_CP_Solenoid04.jpg?1571175368)

The ruler in front of the LED can be used to measure the distance from the light source to its nearest neighbor either left or right. In this case the value is 5.875 inches. The wavelength represented by **λ** (lambda) is equal to the following formula where **d** is the distance between slits in the diffraction grating, **y** is the distance between the LED and its nearest neighbor and **L** is the distance from the slide to the LED.

![](https://cdn-learn.adafruit.com/assets/assets/000/082/444/medium800/light_CP_Solenoid05.png?1571175412)

My diffraction grating slide has a value of **d** equal to 4850nm.&nbsp; If you don’t know this value, it can be determined by solving for d with an LED of a known wavelength.&nbsp; The distance to the nearest neighbor **y** is 5.875 inches and the distance **L** from the slide is 60 inches.&nbsp; The units cancel out so it doesn’t matter if you prefer metric.&nbsp; Plugging in the numbers gives a wavelength of 472nm for the blue led on the surveillance server power button.&nbsp; Therefore, an LED 50nm higher or around 522nm should work well as a detector. I’ll use a Broadcom high intensity green LED with a wavelength of 525nm.&nbsp; It has a narrow 15 degree viewing angle and a water clear lens both of which should improve the sensitivity. More information on measuring wavelength is available [here](https://www.chemedx.org/blog/simple-method-measure-wavelength-light "Simple method to measure wavelength").

# A CircuitPython BLE Remote Control On/Off Switch

## Wiring LED Sensor

Wiring the LED to the nRF52840 feather is very straight forward.

![](https://cdn-learn.adafruit.com/assets/assets/000/082/445/medium800/light_CP_Solenoid06_Adafruit.jpg?1571175760)

The LED will generate a voltage when it detects light. Therefore, the LED anode is connected to A0 which is 1 of the 6 analog to digital pins on the Feather that can read voltages and convert them to a 16 bit value. The Feather’s ground and the LED cathode are connected to a ground rail on the breadboard. As is, this circuit would be very noisy and unreliable. Therefore, a filter is added which is comprised of an 82nF ceramic capacitor and a very weak 15M Ω pull-down resistor. Both are placed between the LED anode and ground. That’s all it takes for the detector circuit. The Feather should now be able to read a voltage change on A0 when a bright blue light hits the attached green LED.

# A CircuitPython BLE Remote Control On/Off Switch

## Solenoid & MOSFET

A mini 5V push-pull solenoid will be used to press the button on the front of the surveillance server.

![](https://cdn-learn.adafruit.com/assets/assets/000/082/446/medium800/light_CP_Solenoid07.jpg?1571175891)

The solenoid is an electromagnet. The metal rod in the center is called an armature. It will be drawn into the solenoid when power is applied to the coil. When power is cut, the spring will return the armature to its starting position. The solenoid draws 1.1A which is much more than the GPIO pins on the Feather can source or sink. Therefore, a logic level N-channel MOSFET will be used to drive the solenoid.

![](https://cdn-learn.adafruit.com/assets/assets/000/082/447/medium800/light_CP_Solenoid08.jpg?1571175957)

The MOSFET can act like a switch.&nbsp; When voltage is applied to the gate pin it allows current to flow between the drain and the source pins.&nbsp; An IRLB8721&nbsp; is selected because its gate can be driven at logic level voltages such as the 3.3V GPIO pins of the nRF52840 Feather.

# A CircuitPython BLE Remote Control On/Off Switch

## Wiring the Solenoid

The solenoid and MOSFET are also easy to wire up.

![](https://cdn-learn.adafruit.com/assets/assets/000/082/448/medium800/light_CP_Solenoid09_Adafruit.jpg?1571176141)

The 5V USB line on the Feather and one lead from the solenoid are connected to a 5V rail on the breadboard. The other solenoid lead is connected to the MOSFET drain pin. The source pin is connected to ground. When enough voltage is applied to the gate, it will complete the circuit and the solenoid will activate. The gate is controlled by GPIO 13. An optional 10K ohm resistor pulls the gate to ground. This mitigates coupling capacitance and ensures the solenoid will not activate if the gate is allowed to float.

A 1N4001 or 1N4004 snubber diode is placed as close as possible to the solenoid leads with the cathode on the positive side. The diode protects the circuit. When power is cut to the solenoid, the electromagnetic field collapses and can generate a substantial flyback voltage which could damage the MOSFET or other components. The snubber diode dissipates this current.

# A CircuitPython BLE Remote Control On/Off Switch

## PCB

Here’s the breadboard components converted to a small 45.5mm x 35.5mm PCB board design using the free version of [Autodesk Eagle](https://www.autodesk.com/products/eagle). The files will download via the green button below and are also [on GitHub](https://github.com/adafruit/Adafruit_Learning_System_Guides/tree/master/BLE_Client_Server/Eagle).

[Eagle files for making a custom PCB](https://github.com/adafruit/Adafruit_Learning_System_Guides/tree/master/BLE_Client_Server/Eagle)
![](https://cdn-learn.adafruit.com/assets/assets/000/082/449/medium800/light_CP_Solenoid10.png?1571176963)

Two female 2.54mm headers (12 & 16 pin) accommodate the Feather. Two male 2.0mm JST PH shrouded connectors (2 pin) receive the LED and solenoid. I etched the board myself using a laser printer and muriatic acid.

![](https://cdn-learn.adafruit.com/assets/assets/000/082/450/medium800/light_CP_Solenoid17.jpg?1571177210)

I've posted my [etching instructions](https://www.rototron.info/pcb-etching-notes/ "Etching Notes"). If you don't fell like doing it yourself there are many online services that can prototype a PCB for a very reasonable fee.

## Alternate Circuit Board - Perma Proto

You could also skip the design and just use an Adafruit [perma-proto board](https://www.adafruit.com/product/1609 "Adafruit Perma-Proto Half-sized Breadboard PCB").

[Female header pins](https://www.adafruit.com/product/598), cut to the right length with diagonal cutters, make for a socket to plug in the Feather board for possible changes.

For the rest of the circuit, move the parts and wires from the breadboard to the corresponding places on the Perma Proto and solder on, it's that simple!

Danger: 

# A CircuitPython BLE Remote Control On/Off Switch

## 3D Printed Brackets

The free version of [SketchUp](http://www.sketchup.com "SketchUp") was used to design a brackets to hold the PCB to the front of the surveillance server. Click the green button below to download the files or see them [on GitHub](https://github.com/adafruit/Adafruit_Learning_System_Guides/tree/master/BLE_Client_Server/SketchUp).

[SketchUp files for the bracket](https://github.com/adafruit/Adafruit_Learning_System_Guides/tree/master/BLE_Client_Server/SketchUp)
![](https://cdn-learn.adafruit.com/assets/assets/000/082/451/medium800/light_CP_Solenoid11.jpg?1571177402)

There’s a slot to secure the solenoid over the push button. On the side is a through hole for the LED light detector. The bracket will slide over the front of the server. Threaded rods will mount through the holes on the side and secure it to another bracket in the back. The PCB will snap into the middle section and is restrained by top and bottom clips.

I 3D printed the brackets using black PLA filament to match the surveillance server color. The mini 5V push pull solenoid fits snugly into the slot on the bracket, but just to make sure it doesn’t wiggle loose, I also printed a small cover plate to secure it. The plate is secured with a screw that self-taps into the hole above the solenoid. When activated, the solenoid will only travel a few millimeters but it’s enough to engage the server’s power button.

![](https://cdn-learn.adafruit.com/assets/assets/000/082/452/medium800/light_CP_Solenoid12.jpg?1571177530)

M5 x 250mm fully threaded stainless steel rods connect the brackets. The assembly slides over the server and the nuts are tightened to hold it in place. The design affords easy adjustments to ensure the solenoid and LED are correctly positioned over the illuminated power button.

# A CircuitPython BLE Remote Control On/Off Switch

## Server Code

The server code requires the following libraries from the [CircuitPython Bundle](https://github.com/adafruit/Adafruit_CircuitPython_Bundle "CircuitPython Bundle"):

- **adafruit\_ble**
- **adafruit\_bluefruit\_connect**

Danger: 

The latest version can be obtained from the [Adafruit CircuitPython BLE repo](https://github.com/adafruit/Adafruit_CircuitPython_BLE "Adafruit CircuitPython BLE repo").

Plug in one Feather nRF52840 to your computer via a known good USB data + power cable. The board should show up as a disk drive named **CIRCUITPY**. [Use this guide to update CircuitPython](https://learn.adafruit.com/introducing-the-adafruit-nrf52840-feather/circuitpython) - you will want the latest version 5.0 or above.

Download and copy the two aforementioned libraries to the **lib** folder on the Feather.

Here's the server code for the BLE peripheral. Click the download link and save to your computer. Then copy the **server.py** file to **code.py** on the Feather.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/BLE_Client_Server/server/code.py

# A CircuitPython BLE Remote Control On/Off Switch

## Remote Control

The remote control client will just be another Adafruit Feather nRF52840 Express board. The board comes with a built in NeoPixel and a push button switch so no external components will be required. It also has a LiPo battery jack and charging circuit so the remote control can be battery operated.

![](https://cdn-learn.adafruit.com/assets/assets/000/082/453/medium800/light_CP_Solenoid13.jpg?1571178072)

Adafruit provides free [modular case designs](https://www.thingiverse.com/thing:2209964 "Case Designs") on Thingiverse to house the Feather boards. The STL files are easy to modify and 3D print.

Here are the 3D printed parts for the remote control.

![](https://cdn-learn.adafruit.com/assets/assets/000/082/454/medium800/light_CP_Solenoid14.jpg?1571178160)

The bottom container holds the battery and an on/off switch. The default design has a mini slide switch. I modified the [design](https://www.rototron.info/wp-content/uploads/CP_Solenoid_SketchUp.zip "Modified case design and brackets") to a mini rocker because I prefer the more defined throw. When the switch is turned on the battery will power the Feather.

The 2 modular containers snap securely together. I drilled 2 holes in the grey case lid. I inserted a black nylon screw through one hole and secured it with a nylon stand-off. It will actuate the built-in push button switch on the Feather. The other hole holds a segment of 2mm end glow fiber optic filament. This acts as light tube for the built-in NeoPixel on the Feather.

![](https://cdn-learn.adafruit.com/assets/assets/000/082/455/medium800/light_CP_Solenoid15.jpg?1571178278)

# A CircuitPython BLE Remote Control On/Off Switch

## Client Code

The client code requires the following libraries from the [CircuitPython Bundle](https://github.com/adafruit/Adafruit_CircuitPython_Bundle "CircuitPython Bundle"):

- **adafruit\_ble**
- **adafruit\_bluefruit\_connect**
- **adafruit\_debouncer**
- **adafruit\_fancyled**
- **neopixel**

Danger: 

The latest version can be obtained from the [Adafruit CircuitPython BLE repo](https://github.com/adafruit/Adafruit_CircuitPython_BLE "Adafruit CircuitPython BLE repo").&nbsp;

Plug in one Feather nRF52840 to your computer via a known good USB data + power cable. The board should show up as a disk drive named **CIRCUITPY**. [Use this guide to update CircuitPython](https://learn.adafruit.com/introducing-the-adafruit-nrf52840-feather/circuitpython) - you will want the latest version 5.0 or above.

Copy the 5 libraries to the **lib** folder on the Feather.

Here's the client code for the BLE peripheral. Click the download link and save to your computer. Then copy the **client.py** file to **code.py** on the Feather.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/BLE_Client_Server/client/code.py

The program scans for BLE peripherals and connects once the target is located.

The user switch is debounced. Generally speaking, when a mechanical switch is pressed, it doesn’t just change from open to closed. The metal contacts oscillate for a few milliseconds. This can trick a microcontroller into reading multiple presses. The debouncer suppresses these erroneous readings. When the switch is pressed a button packet is sent to the server to activate the solenoid.

Next, the code monitors the UART buffer for color packets and uses FancyLED to generate smooth LED color fading for the corresponding color palette. The remote control fades green/aqua if the server is on. Otherwise, it fades red/orange.


## Featured Products

### Adafruit Feather nRF52840 Express

[Adafruit Feather nRF52840 Express](https://www.adafruit.com/product/4062)
The **Adafruit Feather nRF52840 Express** is the new Feather family member with Bluetooth® Low Energy and _native USB support_ featuring the nRF52840!&nbsp; It's our take on an 'all-in-one' Arduino-compatible + Bluetooth® Low Energy with built-in USB...

Out of Stock
[Buy Now](https://www.adafruit.com/product/4062)
[Related Guides to the Product](https://learn.adafruit.com/products/4062/guides)
### Mini Push-Pull Solenoid - 5V

[Mini Push-Pull Solenoid - 5V](https://www.adafruit.com/product/2776)
Solenoids are basically electromagnets: they are made of a coil of copper wire with an armature (a slug of metal) in the middle. When the coil is energized, the slug is pulled into the center of the coil. This makes the solenoid able to pull (from one end) or push (from the other).

This...

In Stock
[Buy Now](https://www.adafruit.com/product/2776)
[Related Guides to the Product](https://learn.adafruit.com/products/2776/guides)
### Lithium Ion Polymer Battery Ideal For Feathers - 3.7V 400mAh

[Lithium Ion Polymer Battery Ideal For Feathers - 3.7V 400mAh](https://www.adafruit.com/product/3898)
Lithium-ion polymer (also known as 'lipo' or 'lipoly') batteries are thin, light, and powerful. The output ranges from 4.2V when completely charged to 3.7V. This battery has a capacity of **400mAh** for a total of about 1.9 Wh. If you need a larger (or smaller!)...

In Stock
[Buy Now](https://www.adafruit.com/product/3898)
[Related Guides to the Product](https://learn.adafruit.com/products/3898/guides)
### N-channel power MOSFET

[N-channel power MOSFET](https://www.adafruit.com/product/355)
When you need to switch a lot of power, N-channel MOSFETs are best for the job. These FETs can switch over 60A and 30V and are TO-220 packages so they fit nicely into any breadboard or perfboard. Heat sinking is easy with TO-220's, but because of the very low Rds(on) of down to 0.009 ohms...

In Stock
[Buy Now](https://www.adafruit.com/product/355)
[Related Guides to the Product](https://learn.adafruit.com/products/355/guides)
### Adafruit Perma-Proto Half-sized Breadboard PCB - Single

[Adafruit Perma-Proto Half-sized Breadboard PCB - Single](https://www.adafruit.com/product/1609)
Customers have asked us to carry basic perf-board, but we never liked the look of most basic perf: it's always crummy quality, with pads that flake off and no labeling. Then we thought about how people **actually** prototype - usually starting with a solderless breadboard and...

In Stock
[Buy Now](https://www.adafruit.com/product/1609)
[Related Guides to the Product](https://learn.adafruit.com/products/1609/guides)
### 36-pin 0.1" Female header - pack of 5!

[36-pin 0.1" Female header - pack of 5!](https://www.adafruit.com/product/598)
Female header is like the duct tape of electronics. Its great for connecting things together, soldering to perf-boards, sockets for wires or break-away header, etc. We go through these guys real fast, and thought that given how handy they are, we'd offer them in a pack of five!  
<br...></br...>

In Stock
[Buy Now](https://www.adafruit.com/product/598)
[Related Guides to the Product](https://learn.adafruit.com/products/598/guides)
### Through-Hole Resistors - 10K ohm 5% 1/4W - Pack of 25

[Through-Hole Resistors - 10K ohm 5% 1/4W - Pack of 25](https://www.adafruit.com/product/2784)
ΩMG! You're not going to be able to resist these handy resistor packs!&nbsp;Well, axially, they&nbsp;do all of the resisting for you!

This is a **25 Pack of 10K Ω Resistors.** More specifically, they are **carbon film** , through-hole...

In Stock
[Buy Now](https://www.adafruit.com/product/2784)
[Related Guides to the Product](https://learn.adafruit.com/products/2784/guides)
### 1N4001 Diode - 10 pack

[1N4001 Diode - 10 pack](https://www.adafruit.com/product/755)
This here is a 10 pack of the classic 1N4001 power blocking diode. These are good for reverse polarity protection (put it between your DC power jack and circuitry to avoid a negative-voltage that would zap your delicate electronics), kickback protection (place across your solenoids, relays...

In Stock
[Buy Now](https://www.adafruit.com/product/755)
[Related Guides to the Product](https://learn.adafruit.com/products/755/guides)

## Related Guides

- [Introducing the Adafruit nRF52840 Feather](https://learn.adafruit.com/introducing-the-adafruit-nrf52840-feather.md)
- [NeoPixel Badge Lanyard with Bluetooth LE](https://learn.adafruit.com/bluetooth-neopixel-badge-lanyard.md)
- [TRON DISC](https://learn.adafruit.com/trondisc.md)
- [DIY Darksaber Build - 3D Print with PropMaker FeatherWing and BLE](https://learn.adafruit.com/ble-darksaber-propmaker.md)
- [Spinning Disc Step Sequencer](https://learn.adafruit.com/spinning-disc-step-sequencer.md)
- [DIY Desktop Calculator with CircuitPython ](https://learn.adafruit.com/diy-rpn-desktop-calculator-with-circuitpython.md)
- [Trellis Feather DSP-G1 Synthesizer](https://learn.adafruit.com/feather-trellis-dsp-g1-synthesizer.md)
- [Pulse Oximeter Wireless Data Logger](https://learn.adafruit.com/pulse-oximeter-wireless-data-logger.md)
- [Multi-tasking with CircuitPython](https://learn.adafruit.com/multi-tasking-with-circuitpython.md)
- [BLE Sniffer with nRF52840](https://learn.adafruit.com/ble-sniffer-with-nrf52840.md)
- [Pathfinder Robot Companion](https://learn.adafruit.com/pathfinder.md)
- [NeoTrellis Feather Case Assembly](https://learn.adafruit.com/neotrellis-feather-case-assembly.md)
- [Busy Box Interruption Sign](https://learn.adafruit.com/busy-box-interruption-sign.md)
- [Four Channel Audio Passive Stereo Mixer](https://learn.adafruit.com/four-channel-audio-passive-stereo-mixer.md)
- [Overwatch Prop Gun: Lucio's Blaster Pt. 3](https://learn.adafruit.com/overwatch-prop-gun-lucios-blaster-pt-3.md)
- [Pocket PiGRRL](https://learn.adafruit.com/pocket-pigrrl.md)
