#Final Project: Magic Glowing necklaces ##(aka Will I fail my other final because I've been tinkering with RN4871s instead of studying?) <a href="final_project_log.html"> Project Log </a> ### Initial Plan For my final project I wanted to make two necklaces that would glow in each other's presence, one for me and one for my boyfriend. The initial design parameters for my final project were as follows: * Packaging: Opaque plastic just big enough to fit all the hardware components, which can be disassembled and assembled easily, and through which the PCB can't be seen but the LED light can. * Hardware: Powered by button cell batteries for portability * Input: BLE connection between the two necklaces. Optimally, both the BLE module and the microcontroller should operate on low power mode. * Output: Glow with brightness proportional to signal strength, as a proxy for distance. ### End Product What I have right now: <embed src="media/video.mp4" width="640" height="360"/> * Packaging: I'm pretty pleased with how this came out! I still need to figure out a good way to put the housing together - either bolts and nuts, or some kind of clasp. I also need to find/3D-pring a chain to thread through one of the holes. * Hardware: I didn't have a chance to connect the button cell batteries, so for now both necklaces are tied down to an FTDI. In fact, they're both connected together because unfortunately, I only have access to one 3.3V FTDI right now. More on why I needed 3.3V FTDIs later. However, de-powering one of the modules sufficiently simulates moving it to a distance far enough for disconnection. * Input: They do connect! I didn't get a chance to implement low power mode, however. * Ouput: The necklace's LED lights up when there is a BLE connection to the other necklace, fades when there isn't, and lights up again when the connection is re-established. I unfortunately picked a pin that wasn't compatible with analogWrite. Also, try as I might one of boards kept giving me RC=-1 when I tried to program it, so only one of the necklackes actually glows. **Project Design Summary** * 2D design: lasercutting the acrylic rings for the housing * 3D design: CADing the necklace pendant * subtractive fabrictation: making the wax mold for the necklace pendant * additive fabrication: casting plastic resin, I guess? * microcontroller interfacing and programming: ATMega328p programmed with AVRISP through Arduino IDE to communicate using serial with RN4871 * system integration and packaging: tape and copper wire, for now ### References There wasn't any one project in particular I worked from, since the design was pretty straightforward, but rather a collection of resources. * A wonderful <a href="http://www.martyncurrey.com/arduino-with-rn48701/">beginner's guide to RN4871</a>, introduced to me by Will Langford * <a href="https://github.com/MCUdude/MiniCore">An Arduino core implementation for ATMega328p</a> that allowed me to program it with the Arduino IDE * The <a href="http://ww1.microchip.com/downloads/en/DeviceDoc/50002466B.pdf">RN4871 user guide</a> * The <a href="https://www.sparkfun.com/datasheets/Components/SMD/ATMega328.pdf">ATMega328p datasheet</a> * The extensive microcontroller-programming and oscilloscope-using library of knowledge that is Ben Yuan's amazing brain The crux of the project came down to figuring out the communication between the microcontroller and the RN4871. ### The Design #### Circuitry and Hardware I based my circuit design off Neil's RN4871 basic board, as well as my previous designs for ATtiny44. I wish I'd found Neil's ATMega example board, but fortunately most things were fairly similar. I connected the Tx of the RN4871 to the Rx of both the ATMega328p and the FTDI, and likewise for the Rx to two Tx's. There were some concerns that this might confused the RN4871, but luckily it seems like it doesn't. I add an LED to the RN4871 as instructed in the RN4871 intro guide, which provides an indication that the RN4871 is working. Sometimes it blinks once and sometimes twice...I have yet to figure out why. From time to time, the RN4871 needs to be rebooted by connecting the reset pin to GND to get the light blinking and it communicable from serial monitor. <img src="media/rn4871_led_traces.jpg"/> #### Programming I started off with using the serial monitor in the Arduino IDE, following the intro guide, to figure out how each RN4871 command worked and what responses I would be given. Then it was time to get dirty with the actual coding. The oscilloscope really came in handy here, as it was able to confirm that we were sending the right ASCII-encoded digital signal to the RN4871 from the ATMega, and subsequently that we were getting responses back. <img src="media/scopeD.jpg"/> I used the SoftwareSerial library (the signals sent when using the Serial library were weak, only 1.5V, too low for the RN4871 to register), using the print() function to send RN4871 commands without carriage return and println() to send commands with carriage return. listen() told the ATMega to listen for data with a buffering and interrupt implementation, available() provided the number of real (not -1 characters that were waiting to be read, and read() read each character one by one, which I appended onto a string, which would be converted to a char pointer, and thence to an integer. The idea is to have the ATMega tell the RN4871 to connect to the other by its MAC address, then in a loop, ask it for the signal strength of its communication with the other module (command M). If the signal strength (in dbm) was negative, then a connection was established; if it wasn't (if the string built from the received characters was ERR) then there is no connection. Ben found from an internet search that lowering the RN4781 baud rate from the default 115200 to 9600 would allow the ATMega to read the signal more easily, and not get a load of garbage. (Neil says that 115200 is way too fast for the Software Serial library). He also suggested adding a 100ms delay between commands from the ATMega to the RN4871, which magically made the serial communication work. Finally, he suggested adding code right after starting the listening that reads any characters that are present due to noise-ish kind of thing, before we send the command to call for signal strength. <img src="media/code1.jpg"/><img src="media/code2.jpg"/> (In my code I use command C to initiate connection; this is because I had bonded the two devices beforehand with serial monitor.) Something I realized when trying to make it so that connection between two RN4871s could be re-established after disconnection was that 1) the module needs to be rebooted and 2) there's a delay after connection for the device to realize that it's connected, so I needed to add a 1s delay before I had the ATMega ask the RN4871 for the signal strength (command M). #### Pendant (housing) This part was really fun! I used my Oomoo mold from back in molding and casting week, and used <a href="https://www.smooth-on.com/products/smooth-cast-326/">Smooth-On Cast 326 plastic resin</a>, mixed with Color Match pigments. It's so pretty! ### Things that went wrong * I forgot to connect the ATMega AVCC to VCC in my routing, so I had to add a wire for that afterwards * Even though I had the RN4871 (rated up to 3.6V) powered by 3.3V regulator regulated power, I had the ATMega powered directly from the FTDI, which for an FTDI with 5V signaling voltage, meant that some of the voltage leaked through and my RN4871 was getting 4V - too high. It actually did sometimes work; other times it just burned out. I tried to fix this by severing the original ATMega VCC connection and wiring it to the trace after the regulator, which worked for a while on one of the boards to get 3.3V; then it went back up to 3.8V, which is still a bit too high. * I still don't know why the ATMega on one of the boards refuses to be programmed. By the end I had really messy soldering, so that could have something to do with it. ### List of materials (all from fab lab inventory unless otherwise indicated): * CR2032 button batteries x2 (Not used yet, bought from Amazon) ($0.94) * RN4871 modules x2 ($7.24) (I actually used up like 5 of these) * ATMega328p x2 ($2.14) * Blue LED, for RN4871 function indication x2 * White LED, for connection indication x2 * 3.3V Regulators x2 * PCB, resistors, capacitors Total cost: ~$25 ### Remaining Work/Questions * Why do I need to reset the RN4871 every time I power cycle to get it working? (At least I don't need to re-upload the code) * Make sure that it actually works by disconnecting/connecting from a distance * Connect to button cell battery * Figure out low-power mode for RN4871 and ATMega * Add a chain ### Files for this project * <a href="files/Katara_necklace.stl">wax mold STL</a> * <a href="files/acrylic_ring.dxf">acrylic ring DXF</a> * <a href="files/rn4871.ino">Arduino code</a>