Watchdog: Herding Cats Has Never Been Easier
By Braddock Bresnahan for Mouser Electronics
I don’t have kids, but I hear they can be a pain in the rear. My heart goes out to the parents carrying three infants wailing at the top of their lungs (the parents wailing, not the children). It reminds me of a time when I was younger: My older brother, my younger sister, and I (all one year apart) were sitting in the car. I don’t know why but all of us were crying at the same time. My mother was driving and, apparently, we were so annoying that we tipped her over a bit. She pulled over, turned around, screamed, “AAAAAAAHH!” and turned back forward. We all went dead silent. She kept on driving and none of us made another sound for the rest of the ride.
Parenting is a job in itself. Kids can wear you down and down (according to my parents, I was a pro at it). Most parents are willing to take on the extra work, but there is a limit to a person’s energy, and there’s only so many different things one can pay attention to at the same time. Anything that can offload some of that work would benefit both parents and their children. For this project, I wanted to make something that would ease the burden of parenthood. That was the inspiration behind Watchdog.
Imagine taking your kids to the amusement park or mall, where they’ll eagerly wander and explore…amongst the crowds of people and various visual obstacles to keeping them in your line of sight. I remember once as a kid being at a theme park and accidentally following the wrong family around. Watchdog is a wristband like device that is worn by a parent and their child(ren). Whenever the child and parent go beyond a specific range from each other, a vibration motor alarm would go off letting them know that they’ve gone too far. With Watchdog, my parents and I would have immediately known I was trailing off. In this project article, you’ll see how to build Watchdog.
Project Materials and Resources
We recommend gathering the following project materials before getting started:
See also: “Watchdog Project: Materials and Resources (with Links)” in the Mouser Electronics Github for links to resources outside of Mouser.com.
Project Bill of Materials (BOM)
Included in BOM
Included in BOM
- 58Ω resistors (quantity 4)
- 10KΩ resistors (2)
- NPN transistor (1)
Project Code
Hardware
- Soldering iron
- Solder
- Pin headers
- MM Jumper wire (lots of them)
- MF Jumper wire (lots of them)
- FF Jumper wire (few of them)
- Breadboard
- Digital multimeter (optional)
- Oscilloscope (optional)
- USB 2.0 Type A to USB 2.0 micro B cable (at least one)
Software
- Atmel Studio 7 IDP
- Atmel START
- CoolTerm Terminal Emulator (optional)
- RN487x Firmware Update Tool (optional)
- RN487x Firmware (optional)
- Smart Phone App: Bluetooth Smart Discover (optional)
Useful Docs (optional)
Project Technology Overview
Watchdog is an intermediate to advanced level project using the Windows 10 operating system
For this project, we’ve used the following products and technologies, described in the following sections:
ATSAMC21N Xplained Pro
The ATSAMC21N Xplained Pro is an all-inclusive development platform that has almost everything you could want from a development board. It comes with the SAMC21N18A microcontroller, an embedded USB debugger, a huge number of pins to play with, an assortment of oscillators, selectable 3.3 or 5.0 target voltages and much more. The ATSAMC21N Xplained Pro’s plug-and-play relationship with Atmel Studio helps immensely in getting a project rolling.
Atmel Studio
Atmel Studio is the preferred IDP for developing any AVR or SAM microcontroller applications. A host of libraries, drivers and examples for AVR/SAM products are included to make your development life easier. When a board is plugged in, a detail page of the board pops up providing easy new project creation, example projects, documentation and datasheets. Atmel Studio is based on Visual Studio and has a very similar feel and visual design, no pun intended. Applications can be written in C, C++ or assembly. For this project we will be using C.
Atmel START
Atmel START is an easy way to configure AVR and SAM microcontroller products. It allows you to configure any part of the board; drivers, clocks, pins, you name it. The configuration can be pushed directly into Atmel Studio and can be changed at any time with no extra hassle. Drivers can be added, configured and removed with only a few mouse clicks. It has a pin configuration tool that allows you to name each individual pin for easy reference; and change any pin’s function and pull using a simple drop-down menu.
BMI160 Shuttle Board
We will need an accelerometer to sense the activity of the user using the band. Our design of the band has no on or off switch, but rather will wake up when it is moved and fall asleep after minutes of inactivity. The BMI160 Shuttle Board is a good solution to this. It comes with the BMI160 inertial motion sensor and the BMI150 geomagnetic sensor for the full range of motion and orientation sensing. The BMI160 has an intuitive interface that can be accessed over I2C or SPI. It also has programmable interrupt output pins for all kinds of motion events. Any-motion, step-detector, free-fall, just to name a few. For this project, we will only need to use the BMI160.
RN-4870-SNSR
The RN-4870-SNSR is a low power, Bluetooth 4.2 development board. The board features the RN4870 which has an easy to understand and use ascii command interface; just give it a command and it will do its thing. When paired with a command terminal, it makes debugging a breeze. No need to take blind guesses at what has gone wrong when you have all the information already in front of you. An alternative to the RN-4870-SNSR is the cheaper MIKROE-2543, but I would suggest that you go with the RN-4870-SNSR as it is a bit more reliable.
Developing Watchdog
In the following sections, we walk you through the process of developing Watchdog:
- Install Atmel Studio 7
- Configure ATSAMC21N Xplained Pro
- Configure BMI160
- Configure RN4870
Install Atmel Studio 7
To start developing with the ATSAMC21N Xplained Pro, you’ll need to install Atmel Studio. Go to the Atmel Studio 7 webpage (http://www.microchip.com/mplab/avr-support/atmel-studio-7) and scroll down to the downloads section (Figure 1). Both download links will get you the same result in different ways. Since you’re reading this article, you probably have internet access, so you should use the online installer.
Figure 1: Download Atmel Studio
Run the executable as soon as it’s downloaded; the location of the executable does not matter. You will be greeted with an all too familiar terms and conditions agreement; and yes, you will have to agree to them to continue. At the bottom, you can change the desired destination of Atmel Studio (Figure 2). You’ll need a little over 6 GB of disk space for the installation. Even if you choose another installation folder, you still may need to free up space on the C: drive. When you are done, click Next.
Figure 2: Install Atmel Studio
You will need to select which architecture(s) to download with Atmel Studio (Figure 3). You can select any architectures you would like but SMART ARM MCU must be checked to work with the ATSAMC21N. When you are done selecting, hit the next button.
Figure 3: Check SMART ARM MCU before continuing.
You are given the option to download Atmel software framework and example projects. It isn’t necessary but can be useful if you plan on working with other Atmel products. Hit Next and then Install. This might take some time.
Configure ATSAMC21N Xplained Pro
We will need to configure the ATSAMC21N Xplained Pro, the BMI160 Accelerometer and the RN4870 for this project. First, install the drivers for the ATSAMC21N Xplained Pro:
- Open Atmel Studio
- Plug in the ATSAMC21N Xplained Pro using the micro USB port.
Atmel Studio will automatically detect the board. As soon as you see a page pop up with the board’s name and information, we’re ready to start (Figure 4).
Figure 4: We’re now ready to begin.
At this point, the code and START configuration provided in the Mouser Electronics Github will be sufficient; but in case you wish to expand on or change a few settings, I’ll run through what Atmel START can do. If you don’t have any interest, you can skip ahead to the next step.
This section is organized as follows:
Starting A New Project
If you wish to start an entirely new project, you can do so easily by using the ATSAMC21N Xplained Pro’s page in Atmel Studio. To start a new project:
- Make sure the environment is set up:
- Atmel Studio is open
- The SAM C21N Xplained Pro page is up.
- Click New Atmel START project using this board… on the SAM C21N Explained Pro page.
- On the page that popped up, select SAM C21N Explained Pro and hit Create New Project.
- Configure your project to your hearts content (can be changed later).
- When you’re done, hit Export Project at the top right of the page.
- Change Specify file name to the desired project name.
- Click DOWNLOAD PACK (will download a file).
- Open the file.
Opening the file will open a new instance of Atmel Studio with a project importer window.
- Click OK on the importer window.
The project will create and open itself, so we can… START developing (I’m not sorry).
Re-Configuring A Project
To re-configure an already existing project:
- Open the project in Atmel Studio.
- Right click the project in the Solution Explorer.
- Click Re-Configure Atmel START Project.
- When you are done, click GENERATE PROJECT.
- A window will pop up, click OK.
- The board has now been reconfigured.
Configure Drivers
When you’re in an Atmel START project, on the left there is a section called "DASHBOARD." Click that if it is not already selected (blue). Here, we can add, remove, and configure drivers and components for the ATSAMC21N Xplained Pro to use (Figure 5). After we add a software component to the project, we can configure it by selecting it; under the diagram it will list all the configuration options that component has. We can get information, rename, or remove it with the buttons to the left of the configuration.
Figure 5:The START dashboard lets you and configure board components
Configure Pins
On the left-hand side of the dashboard, click on the PINMUX tab. Here, we can configure every pin on the ATSAMC21N18A microcontroller (Figure 6). You can configure a pin be selecting it in the list or by clicking it on the diagram. We can give each pin a unique name to easily reference the pin while coding. We can also select input/output pin modes as well as pull-up or pull-down resistors for each pin. Some pin functions are set using the driver configurations in the dashboard (I2C pins, UART pins, etc.).
Figure 6: The START pinmux lets you set all pins’ functions
Configure Clocks
The CLOCKS tab, on the left, allows you to configure… the clocks. Here, we can configure the internal and external oscillators as well as configure the clocks used by the ATSAMC21N Xplained Pro (Figure 7). We can add or remove oscillators and clocks by checking the box next to their names. We can also configure each by clicking the gear under their name. Atmel START shows you which oscillators and clocks are needed for the board with dotted arrows and dynamically changes when components are added or removed.
Figure 7: The START clocks lets you configure oscillators and clocks
Configure BMI160
Next, we will configure the BMI160. Congratulations! We are now done configuring the BMI160 (it doesn’t need configuring). Head on to the next section.
Configure RN4870
You only need to configure the RN4870 if you’re planning to use the RN4870 Firmware Update Tool or CoolTerm for debugging. If you skip this section, you will need to find the Bluetooth addresses of each band and write them in usart.h in BLE_DEVICE_ADDRESS.
Some instructions might change depending on if you are using the RN-4870-SNSR or the MIKROE-2543. For these instructions, we will focus on the RN-4870-SNSR:
Install Driver
Before anything else, the RN4870 driver needs to be installed. To install the driver:
- Make sure the RN4870 is not in developer mode by setting the switch, at the bottom of the board, to 1 (OFF).
- When set to ON (ON) the RN4870 is in developer mode and, if plugged into the computer, may corrupt the driver installation.
- If the status light, when powered, is solid red, then it’s in developer mode.
- If the driver is corrupted, you will need to uninstall the driver and run through the process again; which can be problematic if you don’t have administrative privileges on your work computer (I learned the hard way).
- Plug in the RN-4870-SNSR using the micro USB port on the board.
- The driver will install automatically when plugged in.
Update Firmware
- Download the firmware update tool: http://ww1.microchip.com/downloads/en/DeviceDoc/RN487x%20Firmware%20Update%20Tool%20-IsUpdate.zip.
- Download the latest firmware: https://www.microchip.com/wwwproducts/en/RN4870
- Set the RN4870 to developer mode (flip the switch at the bottom to ON).
- Plug in the RN4870. The status light should be solid red. If not, press the reset button on the board.5. Run the firmware update tool (Figure 8).
- In the Access Port section, of the firmware update tool, at the top, select the serial port the RN4870 is on. You can tell which serial port it’s using by disconnecting and reconnecting the board. If you see a port disappear and reappear, that’s your one.
- Set the baud rate to 115200 (the default RN4870 baud rate).
- Set memory type to flash.
- Set memory subtype to Embedded.
- Hit the Connect button. It will inform you if the connection succeeded or failed. There are multiple reasons why a connection could fail:
- The board is not plugged in.
- The wrong port is selected.
- A setting in the Access Port is incorrect.
- The RN4870 is not in developer mode.
- The driver on the computer for the RN4870 is corrupt.
- Click the Browse button and select all the RN4870’s firmware images (all at once).
- After the images are selected, click Update to start the update. This can take a few seconds.
- After the update, click the Disconnect button at the top (where Connect used to be).
- Disconnect the board from the computer.
- Flip the switch to 1 (OFF) to leave developer mode (applies next power on or reset).
Voila! The firmware is updated.
Figure 8: This is the UI for the RN4870 Firmware Update Tool
Configure CoolTerm
CoolTerm is a great debugging tool to use with the RN4870. Since the RN4870 uses an ascii command interface, everything the module can do we can do through CoolTerm. To setup CoolTerm:
- Download CoolTerm Terminal Emulator (should be a .zip file or similar).
- Extract CoolTerm to any desired location.
- Plug in the RN4870.
- Make sure it is not in developer mode.
- Run the CoolTerm executable (Figure 9).
- Click Options… in the Connection category at the top (next to Edit and View) (Figure 10).
- Set the port of the RN4870.
- Set the baud rate to 115200 (the default RN4870 baud rate).
- Click OK.
You can now connect to the RN4870 at your leisure.
Figure 9: This is the UI for CoolTerm
Figure 10: This is the right place to configure options
Find Bluetooth Address
Due to technical limitations in the project code, we need to change a hard-coded Bluetooth address in usart.h to our RN4870’s address. To do this, we will need to first find its address:
- Plug in the RN4870 (not in developer mode)
- Click Connect.
Sometimes you will see, in the console, a ӱ character appear. It doesn’t mean anything; but is a useful, but not definitive, indicator that the connection succeeded.
- On the keyboard, type “$$$” (the characters will not appear in the console).
- When a command prompt appears, type “D” (capital) then hit enter.
- The console will populate with the device information. The address should be the 12-digit hexadecimal number beside BTA.
When we find the address, we need to write it down in usart.h as the BLE_DEVICE_ADDRESS definition. This definition has no functional use, but it will help us remember what band has what address. To be able to connect other bands, we need to write that band’s address in the BLE_PARTNER_ADDRESS definition. We can use the same process to get its address.
Hacking the MIKROE-2543
The MIKROE-2543 does not have the luxury of a micro USB port and will not work with CoolTerm nor the firmware update tool; but if you already have at least one RN-4870-SNSR, you can trick the MIKROE-2543 into working:
- Remove all jumper connectors in the UART pin grouping (J3) on the RN-4870-SNSR.
- The pins connected to the micro USB port are on the inside of the UART pin grouping. Holding the module face up, here are the pins from bottom to top: RX, TX, CTS and RTS:
- Connect the SNSR RX pin to the 2543 RX pin.
- Connect the SNSR TX pin to the 2543 TX pin.
- Connect the SNSR CTS pin to the 2543 RTS pin (if flow control is enabled).
- Connect the SNSR RTS pin to the 2543 CTS pin (if flow control is enabled).
- Plug in the RN-4870-SNSR.
The MIKROE-2543 now works like the RN-4870-SNSR
Hardware Setup
Here is where things get complicated. There are a lot of external parts in this project, so get ready. We’ll be using the pins configured in the Mouser Electronics Github in Watchdog/.atmelstart/atmel_start_config.atstart. It is best practice to have the board disconnected from power while wiring. It’s recommended that we mount the BMI160 and the RN4870 on a breadboard if able.
This section will cover the following:
Power
Power needs to be supplied to the BMI160, the RN4870, and the Vibration motor; everything else can be handled by pins:
- Make high and low power rails for easy access to VCC and GND.
- You should use the VCC and GND pins at the top of the board (near the micro USB).
- Connect the VCC pin on EXT2 to VDD (pin 1) on the BMI160 Shuttle Board (BMI160 SB). See the BMI160 SB datasheet for the pinout.
- Connect a GND pin on EXT2 (there are two) to GND (pin 3) on the BMI160 SB.
- Connect the VCC pin on EXT3 to any pin in the VBAT grouping (J10) on the RN-4870-SNSR (RN4870). The RN4870 schematic has more details.
- Connect a GND pin on EXT3 (there are two) to any pin in the GND grouping (J2) on the RN4870.
If you are using the MIKROE-2543, 3.3V and GND are marked on the board; pretty strait forward.
Vibration Motor
The vibration alarm is the second most important external component (behind the RN4870). The motor cannot be powered by an output pin on the board, as they are current limited, so we will use a transistor to give it more juice. To mount the motor to a breadboard, you’ll need to solder:
- Solder the leads of the motor to the short sides of some pin headers.
- Mount the pin headers (with the motor attached) on the breadboard.
- Connect the blue lead of the motor to ground.
- Mount an NPN transistor and connect the emitter end to the red lead of the motor.
- Connect the collector end of the transistor to VCC.
- Connect pin PC27 (EXT1) to the base pin of the transistor.
LEDs
The LEDs are to help the user understand what the band is doing. There are four LEDs total:
- Mount three green LEDs and one RGB LED to the breadboard.
- Connect pin PA10 (EXT1) to the first green LED.
- Connect pin PA11 (EXT1) to the second green LED.
- Connect pin PA18 (EXT1) to the third green LED.
- Place a 58Ω resistor on each led and connect them to ground.
- Mount an RGB LED to the breadboard. The LED pin order is green, blue, cathode, red, with the cathode being the longest in length.
- Connect pin PB01 (EXT2) to the green lead.
- Connect pin PB02 (EXT2) to the blue lead.
- Connect pin PB00 (EXT2) to the red lead.
- Place a 58Ω resistor from cathode to ground.
BMI160
We need to communicate with the BMI160 to configure motion events and interrupts. We will be using I2C to communicate with the module (see the BMI160 SB datasheet for pinout):
- Hard wire VDDIO (pin 2) to VDD (pin 1). Uses VDD for I2C voltage.
- Hard wire CS (pin 7) to VDD (pin 1). Sets serial mode to I2C.
- Make two separate 10KΩ pull-up resistors to VDDIO (pin 2) on your breadboard.
- Connect pin PA16 (EXT2) to the first pull-up resistor.
- Connect pin PA17 (EXT2) to the second pull-up resistor.
- Connect SDA (pin 17) on the BMI160 to the first pull-up resistor.
- Connect SCL (pin 18) on the BMI160 to the second pull-up resistor.
- Connect pin PC27 (EXT2) to INTB/IO6 (pin 20) on the BMI160.
RN4870
The RN4870 uses UART to communicate with external devices. The pins for the RN-4870-SNSR are different than the pins of the MIKROE-2543:
RN-4870-SNSR Instructions
To wire UART on the RN-4870-SNSR:
- Remove all jumper connecters in UART group (J3) on the RN-4870-SNSR.
- Connect pin PA09 (EXT2) to HCI_RXD (pin 8 on J3) on the RN-4870-SNSR.
- Connect pin PA08 (EXT2) to HCI_TXD (pin 6 on J3) on the RN-4870-SNSR.
If flow control is enabled (which Watchdog is), there are additional steps to be taken:
- Connect pin PB22 (EXT3) to P36 (pin 4 on J3) on the RN-4870-SNSR.
- Connect pin PB23 (EXT3) to P00 (pin 2 on J3) on the RN-4870-SNSR.
MIKROE-2543 Instructions
To wire UART on the MIKROE-2543:
- Connect PA09 (EXT2) to TX on the MIKROE-2543.
- Connect PA08 (EXT2) to RX on the MIKROE-2543.
If flow control is enabled (which Watchdog is), there are additional steps to be taken:
- Connect PB22 (EXT3) to CTS on the MIKROE-2543.
- Connect PB23 (EXT3) to RTS on the MIKROE-2543.
Expanded Features
Watchdog originally had more features planned but, unfortunately, I was unable to implement them due to time constraints. The code for the project might seem a bit odd at times because I had written them with these expanded features in mind. I couldn’t do them but that doesn’t mean you can’t. Here are some of the features that I was unable to do:
- Multiple band connections: Only being able to connect one band might be annoying to parents with multiple children.
- Scanning for nearby bands: Band connections currently must be hard coded in; so, if a band breaks, the user is hosed.
- Host/Guest (Master/Slave) band relationship: The host band is the master of all connected bands. Only the host band can change range settings or disconnect others from a group. This is to prevent children from messing with the settings.
- Low power detection and handling: It would be bad if a band stopped working without anyone’s knowledge.
Conclusion
The prototype is now complete. Just plug it in, run the code and you’re off. Watchdog solves a rather small problem. Many people would argue that parents should just pay more attention to their children. I can see the reasoning behind that. Most of the time, parents are paying attention; but no mistake happens intentionally; and when it comes to kids, anything that helps avoid mistakes is welcome.
Braddock Bresnahan is a junior at the University of Texas at Arlington where he is majoring in Electrical Engineering and minoring in Computer Science. Growing up with Dyslexia, reading and writing did not come naturally to him; however, he overcame his handicap with hard work and dedication. His love for engineering began in 2014 when he took an introductory engineering course in high school. He learned the engineering design process, discovered a natural proficiency in problem solving, and fostered a love for programming. After high school, Braddock enrolled in Baylor University where he spent three years studying electrical engineering. He later transferred to the University of Texas at Arlington to finish his degree. Braddock spends his free time on personal coding projects and taking care of his pet cockatiel, Kevin.