Thursday, December 30, 2021

Kitty Salon

For Christmas this year, my girlfriend and I made a talking toy cat for her nephew. It was a fun opportunity to build something together and practice some fabrication skills that are useful for calculator projects.

The idea for this project came from her nephew pretending to be a cat and calling himself "Kitty Salon." The toy version of Kitty Salon has five recorded messages saying things like "Meow! Meow!" and "Rise and shine!" A button on the bottom plays one of the messages every time it's pressed, cycling through all five. Two LEDs for eyes glow to show that the cat is on and fade on and off while the cat is talking.

Body
The first step to building the cat was sculpting a model out of clay. The innermost part of the model is a small box made out of foamcore which is large enough to hold all the electronics. The first try at making the cat out of clay from the craft store failed. The box the clay came in said that it dries when exposed to water. In our case, the model cracked in several places after drying and the feet fell off. The second try used Sculpey which doesn't require water to set and has the added benefit of working well with silicone rubber molds. You can see in the picture above that the back end of the cat is a little square and fat since the Sculpey is molded around the box for the electronics.

Silicone mold
After the clay model was finished, I cut pieces of foamcore to surround the model on all four sides so I could pour a silicone rubber mold over the whole cat. Hot glue held the foamcore pieces together while the mold was being poured. After pouring less than half of the mold, the silicone I had on hand ran out. Before buying more, I reconsidered the mold design and realized that the foamcore walls were way too far away from the edges of the model, so a lot of silicone was being wasted on the sides. I took down the foamcore walls and cut away more than an inch of rubber on each side of the unfinished mold then piled those pieces on top of the existing mold. After I put more foamcore up and started pouring again, the new silicone bonded the existing mold to the extra pieces on top, so much less silicone was needed to finish the mold. When the mold level reached the cat's neck, I put a large piece of clay behind the cat's head and another piece on the wall in front of its face. This cut down even more on the amount of silicone required and doesn't seem to have weakened the mold.


After the mold was dry, I removed the model which disintegrated since regular Sculpey does not dry or become rigid. It took some time to get all of the clay out, and I ended up using a wet cloth to try to get the last of it out. Since the cheeks and ears stick out on either side of the head, I had to cut a slit in the side of the mold so the head would come out with the rest of the body when releasing the mold.


Plastic casting
After the mold was finished, I poured enough plastic to fill the head and a few millimeters of the body.  I put a piece of foamcore on either end of the mold and used rubber bands to keep tension on them so no plastic would leak through the slit in the side. The result felt very sturdy and the head was easy to release through the slit. The only obvious defect was missing plastic on the tips of the ears which is the lowest part of the mold. Looking closer, I found water left over from cleaning in the tips of the ears that prevented the plastic from flowing to that part.

Next, I got ready to pour the plastic for the final version of the cat. Since the eyes are LEDs controlled by the electronics in the base of the cat, there needed to be a tunnel through the cat to connect the two pieces. These were formed by covering two bamboo skewers with clay and running one from each eye through the top of the mold. The first pour of plastic was enough for the head and the top of the body but not quite up to the level where the box for the electronics starts. Since the skewers were covered in clay, the plastic didn't stick to them, and removing the skewers left two clean holes from the eyes to the bottom of the cat.

To form the box for the electronics, I reused the foamcore box from the base of the clay model and poured silicone into it to form a plug the same size as the box. Then I put one chunk of clay over the holes leading to the eyes and another in the back of the model so the box sized silicone plug could rest on both of them. The rest of the poured plastic filled around both pieces of clay and around the box plug leaving a space big enough for the electronics without disturbing the holes leading to the eyes. This was enough to finish the plastic part of the cat. After I removed the box plug, I cleaned out the clay sealing the holes leading to the eyes so there was a clean path from the eyes to the space holding the electronics.

Bottom cover
Casting the cat in plastic was a neat experience that let me try out some things I want to use for making calculator cases. I still needed a way to hold the electronics in the base of the cat since the bottom of the cast plastic was open. At the hardware store, I found four small bolts with nuts so I could bolt down all four sides of a plastic cover on the bottom. However, when I got home and tried putting the nuts, batteries, speaker, and electronics into the base, I saw there was no possible way to fit it all in. After some tinkering, I realized it would be possible to make a strong base cover with only one of the bolts which would leave enough room for everything else. Here are the parts designed in OpenSCAD that I printed on my 3D printer:


The white part on the left is the bottom cover. The part in the middle is glued to the right edge of the cavity in the base so the two tabs on the cover can slide into them. The single screw going through the bottom cover goes into a nut held by the standoff on the very right which is glued to the bottom of the base cavity. The neat thing about the standoff is that I didn't need to shorten the bolt to fit the cavity exactly since the bolt reaches far enough down to go all the way through the nut with any excess bold length going into the center hole in the standoff. It took some careful adjustment to get everything lined up, and now everything is very solid and feels strong. The grid of holes at the top of the cover are for the sound from the speaker. The square slot and round hole on the left of the cover are for the power switch and button that plays the sounds, though I ended up moving those to where the sound holes are when the switch and button wouldn't fit. Designing these was one of the most enjoyable parts of the project and gave me some new ideas for calculator cases. Below is a bottom view of the cat where you can see how the parts fit together:


Electronics
The electronics for the project are pretty simple. Like most of my microcontroller projects, it uses an MSP430. The audio is 8,000 samples per second with each sample being 8 bit, so only two seconds of audio fit in the 16KB flash of the MSP430. The five recordings for the project total about 35 seconds, so the 280KB of audio data is stored on a W25Q32JV SPI EEPROM. This is a really interesting chip with 4MB of storage and a QSPI interface that works at 133MHz which could be really useful for all kinds of projects. Unfortunately, Digi-Key has the DIP8 version I'm using marked as obsolete, so I had to use one of the three I had ordered for other projects.

Since I wasn't sure what kind of speaker would work best, I ordered three 8ohm speakers of different shapes at 0.25W, 0.5W, and 1W. The 0.25W speaker, which was also the largest, sounded noticeably better than the others, though it took a lot of work to get the sound recognizable. Part of the problem was resting the speakers on the table while I was testing since the vibrations caused the speaker to rattle and obscured the sound. After fitting two AAA batteries, the circuit board holding the MSP430 and EEPROM, the power switch, and sound button into the bottom of the cat, there wasn't enough room for the 0.25W speaker, so I had to use the smaller 0.5W one. 

The speaker is driven by a PN2222A NPN transistor since the MSP430 GPIO pins can't supply anywhere near the required current for the speaker. At first, I calculated the base resistor for the transistor so that 0.25W would flow through the transistor, but then I realized that I hadn't taken into account the 8ohm rating of the speaker. Like with other hardware problems I've run into, I was able to find help pretty quickly on IRC and calculate the appropriate resistor size. Unfortunately, when I swapped the 0.25W speaker for the 0.5W speaker, I had already soldered on the resistor for the base of the transistor, so the output is much quieter than I would like. Since only a single transistor is driving the speaker, I think some of the capability of the speaker is lost, though I don't understand much about how an amplifier with multiple transistors works. Some of the designs I saw had a capacitor in series between the MCU pin and transistor base instead of just a resistor which is something I also don't really understand. Like the designs I found online showing how to drive a speaker with just one transistor, I put a diode in parallel with the speaker to protect the circuit from the energy that builds up in the speaker magnet. The resulting sound was fairly scratchy with audible clicks and pops. I read about how a low pass filter could help with this, so I put a capacitor between ground and the transistor base which hopefully smooths out the PWM signal coming from the microcontroller. The calculation for the capacitor size in a low pass filter seems easy enough, but I wasn't sure how to correlate that with what would sound best. Instead, I tried different capacitor sizes until I found out that 0.1uF improves the sound the most.

Soldering the tiny PCB was a little bit of a challenge since I hadn't left myself quite enough room to fit everything comfortably when I built the foamcore box for the base of the cat. Some old tricks from other soldering projects let me fit everything with almost no room left to spare on the board. Below you can see, for example, that the capacitors for the two chips are soldered on the backside of the board and some connections are made of solder on the backside rather than wire on the frontside. Thankfully, I didn't need to make room for a voltage regulator since both the MSP430 and EEPROM can work at 3.6v down to 2.7v provided directly from the two AAA batteries. 



Software
The software part of the project was relatively simple, although I got stuck for a long time in several places. Basically, once a sound starts playing, the microcontroller just needs to fetch an 8-bit sample from the EEPROM and use it to PWM the transistor base 8,000 times a second. The MSP430 runs at 16MHz, though I lowered the speed to 12MHz so it would still work correctly at 2.7v, which is plenty fast enough to feed values to the speaker. The MSP430 lets you set 16-bit values for both the duty and period of the timer which drives the PWM pin. I set the period to 256 since the samples are 8 bits in size. One idea I had was to calculate the period separately for each sample and store that on the EEPROM as well, though I didn't have enough time to try it out. A sample like 128, for example, with a period of 256 provides a 50% duty cycle, though a duty of 1 cycle and a period of 2 would also provide a 50% duty cycle with a potentially smoother average output voltage on the PWM pin. There are other things I could have done that might have improved the audio quality like passing the recording through a software low pass filter before writing it to the EEPROM. Another idea is to handle the transitions between PWM states more gracefully since resetting the level in the middle of a period might be part of why the audio sounds so scratchy. 

After recording the audio on my computer, I exported it as a WAV file using Audacity. Parsing the file to extract the metadata and audio samples was really easy to do in Python. Before setting up the EEPROM, I tested playing one second of audio stored in the MSP430's flash by outputting the sample values from the WAV file to a const array in a C file using Python. The resulting audio was recognizable but very quiet. Looking at the waveform in Audacity, I noticed that the highest and lowest values were far from the minimum and maximum values possible. Since I already had the sample values in memory in Python, I found the minimum and maximum values then scaled all the samples so they used the full range which made the audio much louder. Exporting a signed 16-bit WAV, scaling then converting to 8-bit samples seemed to work the best. One thing I noticed was that playing a solid tone as a test was much louder than playing the scaled audio. Scaling in a different way might produce much louder results, so I'll investigate this if I ever do another audio project.

After the microcontroller starts up, it goes into Low Power Mode 4 (LPM4) waiting on the play button to be pressed. This is the lowest power mode, and the MSP430 can only wake from it on an external event such as a button press. Instead of always keeping the system in LPM4, I put in an on/off switch since I only had one night to work on the interrupts which wasn't enough time to make sure the power consumption of the whole system was low enough in LPM4 not to drain the batteries long term. After the button is pressed and the MSP430 wakes from LPM4, it starts feeding sample values from the EEPROM to the PWM timer. At first, the program waited 1,500 cycles between each sample since this is 1/8,000 of a second when running at 12MHz. However, when I added code to manage the LEDs fading on and off, it took enough extra cycles that the sound was slowed down and distorted. Instead, I used the second timer on the MSP430 to keep the sample times identical regardless of how many cycles the LED code takes. Although I've used timers for a lot of MSP430 projects, I realized that I haven't exactly mastered them since I had a lot of trouble trying to get the second timer working from scratch. Eventually, I looked at an example online and got it working. The chip could go into LPM3 to save power while the sample is playing and the second timer is counting down, though I have it polling the timer interrupt flag instead. This wastes energy but was easier to get working since I was running out of time. 
 
Conclusion
This was a really fun project and a great opportunity to do a microcontroller project after taking a long break. Designing the 3D printed cover and casting the cat in plastic are both good practice for building calculator cases.

No comments:

Post a Comment