Train Horn Synthesis with the MSP430FR2000

Introduction

The hardware and code presented below is designed to simulate the sound of a train horn (or come as close as possible with the given hardware). Specifically is is designed to synthesize the sound of a Leslie S-3L Locomotive Air Horn.

Train_Horn_Main

The final design is battery powered for portability. It uses a 18650 LiPo cell and features an onboard charger using a standard micro USB port and a low battery indicator LED.

Train_Horn_Back

The circuitry is able to drive a standard speaker load of 8-4Ω for mono sound. Four adjustable gain settings of 4, 8, 12, and 16 are available for setting the output volume of the audio amplifier with minimal modifications.

Train_Horn_Hand

An onboard microcontroller generates the chord and enables the user control of the sound though a simple push button. The MSP430FR2000 was chosen due to its low cost in single quantities and availability at the time of designing this product. The MSP430 line was chosen specifically to develop a familiarity with using the product.

Train_Horn_uC


A Brief Overview

train_horn_annoted_pcb_sm

train_horn_annoted_pcb_back

A 4-8Ω speaker is connected to the speaker terminal. The device is powered from a 18650 LiPo cell which is charged using the micro USB port, a blue LED indicates that the battery is charging. The charging circuitry is capable of charging the battery at a maximum current of 500mA.  The low battery detection circuitry will illuminate a red LED indicator light when the battery voltage is low and requires charging. The audio amplifier gain (volume) is configured using jumpers JP3-JP8 and is set to 16 by default. Pressing and holding either the small SMD or the larger right angle play button generates sound. A green LED is illuminated while sound is playing. Releasing the button stops sound generation.

Schematictrain_horn_schematic


Detailed Description

Sound Generation

The Leslie S-3L Locomotive Air Horn generates its distinctive sound using a three tone chord of approximately C, D#, and A (http://atsf.railfan.net/airhorns/s3l.html).

The onboard microcontroller generates the chord using three 50% duty cycle PWM signals. Since the MSP430FR2000 microcontroller has only one general purpose timer the method outlined in TI app note SLAA513A was implemented to generate multiple time bases on a single MSP430 timer module. This enables use of the lower cost MSP430FR2000 instead of having to move to a potentially higher cost MSP430 microcontroller with three separate timer modules.

uC_schematic

Each PWM signal from the microcontroller is passed through a three stage RC low pass filter to produce a sine wave on the output.  The RC low pass filter was designed with the help of the following online RC low pass filter calculator: http://sim.okawa-denshi.jp/en/CRtool.php

filter_amp_schematic.png

After each PWM signal is converted to a sine wave the signals are then mixed together before entering the audio amplifier. The audio amplifier is a TPA6211A1 mono audio power amplifier. The audio amplifier has 4 adjustable gain settings that can be changed by desoldering the smd jumpers (JP3 to JP6) as indicated by the table. For example to set a gain of eight JP4, JP5, JP6, and JP7 would be removed from the PCB. The amplifier can drive a 4-8Ω speaker.

train_horn_lg_speaker.png

Battery Circuitry

The circuit is powered with a standard 18650 LiPo cell. Be sure to observe polarity of the battery holder. The positive end of the battery installs opposite of the speaker terminal. Installing the battery in reverse polarity will destroy the battery charger IC and will damage the remaining circuitry. Don’t ask me how I know.

Train_Horn_PolarityMarks.png

Charging is controlled by the MCP73831 charge management IC at a maximum charging current of 500mA using the micro USB port. This USB port if for charging only, the data lines are not connected to the MSP430FR2000 microcontroller. A blue LED indicator is turned on while the battery charges. The LED turns off to indicate that the battery is fully charged.

charge_schematic.png

Train_Horn_ChargeLED.png
Charging LED

A red LED is used to indicate a low battery voltage. The MIC7221 comparator is used to illuminate the LED when the battery voltage drops below ~3.3V. The internal 1.2V reference from the microcontroller is used as the voltage reference for the comparator.

batV_schematic.png

The onboard comparator of the MSP430FR2000 was not used due to issues with getting it to function correctly, most likely error on my part. To save time and frustration an external comparator was used instead.

Train_Horn_LowBattery.png
Low Battery LED

The MSP430FR2000 microcontroller has a maximum voltage supply rating of 3.6V requiring a 3.3V low dropout regulator to prevent a fully charged LiPo cell voltage of 4.2V damaging the microcontroller. SMD JP1 can be desoldered from the circuit to put the LDO in shutdown mode, turning off the microcontroller.

Train_Horn_LDO.png

Firmware

The microcontroller spends much of its time in a low power sleep mode, the full circuit draws about 0.3mA of quiescent current. A 1600mAh LiPo cell will have a standby life of over 3700 hours or 154 days!

Sound is generated when the user presses and holds down the play button. There are two play buttons on the PCB, a small SMD button and a friendlier medium sized right angle button. The PCB was originally designed to use only the SMD button but a last minute change added the right angle through hole button. Both are populated to allow multiple options of triggering sound generation. The green LED indicator is used to indicate when the sound is being generated.

Train_Horn_PlayLED
Playing LED

Below is the code loaded onto the microcontroller. I have attempted to add descriptive comments for future me to understand. Hopefully it will allow others to understand as well.

Get the code from the Github repository.

Modifications

“That’s cool and all but how do I modify the hardware/firmware.”    – you

I suspect that very few if anyone would ever find a use for this exact project but it does offer some aspects that may be useful elsewhere with little to no modifications.

Generating a Different Chord

The MSP430FR2000 is capable of generating up to a 3 note chord, a limit set by Timer_B having only three capture and compare registers (CCRx).  To generate other frequencies the following formula is used:

TBCCRx = \frac{TimerB_{clk}}{(2*frq)}

TBCCRx is the increment to write to the capture and compare register. TimerB_clk is the clock source for timer_b, which in my code is 32 kHz (binary 32 kHz which is 32,768 Hz).

The RC low pass filter will have to be tuned for the new frequency. Using the online calculator set the frequency and choose a standard capacitor value, something like 100nF which is widely available. The calculator will then suggest a resistance value for your filter. Filtering is not necessary but it does produce a nicer sound than a PWM wave.

Train_Horn_filter.png

As an example if I wanted P2.1 to generate a 523 Hz frequency I would increment TBCCR2 31 by changing line 144 in the code above to TBCCR2 += 31. R10, R13, and R17 from the schematic would change to 3kΩ. For small changes in frequency from the original values you may be able to get away without modifying the RC filter. An oscilloscope will help determine if the output from the filter is close to a sine wave.

Changing Audio Amplifier Gain

Changing the gain to 12, 8, or 4 is possible by removing the corresponding jumpers according to the table on the schematic and the silkscreen. I chose these gain settings since they were the easiest to implement and worked well for my application. A gain higher than 16 will cause the audio signal to be clipped producing distorted sound.

Train_Horn_AmpJP

Gains other than those from the table will require more effort. First remove all the jumpers along with resistors R18-R25.

R18-R21 is connected in parallel to pin 4 of the audio amplifier and R22-R25 is connected in parallel to pin 3 of the audio amplifier. The resistance value connected to pin 4 and pin 3 of the audio amplifier must be the same value.

Gain is calculated using the following equation:

Gain = \frac{40k\Omega }{R_{f}}

For example if I wanted a gain of 10 I would make it so the R18-R21 resistors connected in parallel are equal to 4kΩ as well as the R22-R25 resistors connected in parallel.

A dual gang potentiometer can be connected instead of discrete resistors values but was not included in the final design due to the size of such potentiometers.

Uploading Firmware

Firmware is uploaded to the MSP430FR2000 using the programming header and a MSP430 TI Launchpad. I used a MSP-EXP430FR4133 but other MSP430 Launchpads will work. Remove all the jumpers from the Launchpad eZ-FET header and connect to the programing header 3V3, GND, SBWTDIO, and SBWTCK.  Do not connect 5V power from the Launchpad or damage will occur. If you have a LiPo battery installed make sure to remove the jumper from the EXT 3V3 header before connecting the launchpad. Code is compiled and uploading using Code Composer Studio 7.4.0.

Train_Horn_PRGwiring

Open CCS 7.4.0 to either a new workspace or a previously created workspace. Create a new CCS Project using File -> New -> CCS Project. Select the MSP430FR2000 as the target.

NewCCSProject_Target

Chose a project name, I used train_horn_hold as my project name but anything will work. Select the Empty Project (with main.c) from the project templates and examples before clicking finish.

Replace the code in main.c with the code from my GitHub repository. Modify the code as you wish. To upload code click build (the hammer icon below Navigate) then click debug (the bug icon to the right of build). If the compiler returns with warnings you must fix these before you will be able to upload the code. The best place to search for help is TIs forums. Googling the error message will also yield useful help.

In the debug perspective click the play button to execute code. You can then see register settings, memory values, and more. This is my number one reason for abandoning the easy to use Arduino platform, debugging is amazing!

For more detailed help with CCS than I could ever give visit the CCS Wiki for a getting started guide or search YouTube for tutorials. The Embedded podcast also has a great blog series on getting started with the MSP430 microcontroller family with advice on how to setup and use CCS.


Application Specific Details

Below is a brief explanation of exactly why I built this device and the final product that this went into.

Why?

Have you ever made something only to ask yourself after it is finished, why? Me neither.

I needed to generate a jarring sound for a silly reason. This project started like many other projects with a trash find, a PA speaker that was just begging to be rescued and reused. Slowly this project evolved from a simple weekend or two Arduino based solution to the five week project presented above. Like many of my projects I used this particular one as a learning experience for programing and embedding a microcontroller from the MSP430 family of microcontrollers. In between the start and end of this project I also learned about sound generation using PWM and train horn chords.

After designing the schematic and PCB using KiCAD I sent the gerber files off for manufacture. The minimum order quantity was 5 but I found that ordering 10 PCBs cost the same amount as 5 so I have plenty of spares.

train_horn_3dView

While the PCBs were being manufactured I exported a 3D model of the PCB from KiCAD for export into Fusion 360. From there I set to designing a 3D printable case to attach the PCB to the PA speaker I rescued from the dumpster. Being able to export a 3D model from KiCAD helped a lot with designing the case and made it super easy to have the design finished and printed before the PCBs arrived.

train_horn_exploded3Dcase

PCBs arrived within a week of ordering and I set about assembly. Having also ordered a solder paste stencil, populating the PCB took the most amount of time. I spent about half an hour populating the board, taking care not to mess up any of the resistor and capacitor values. Then it was into my reflow toaster oven for 6 minutes of anticipation. The battery holder, speaker terminal block, programing header, and right angle tactile switch was solder onto the PCB after reflowing the smd components. A quick firmware flash and it was time for the moment of truth. Success! Or more like choo-choo.

Ok, admittedly it does not sound exactly like a train horn but more of a horn in general. There is something else going on with the Leslie S-3L air horn then just the three chords, harmonics possibly? With that said this project is finally done, I accomplished my goal and learned a great deal about using the MSP430 microcontroller. Since I only needed one or two circuits I will list the remaining fully assembled circuits for sale on my Tindie store, just in case you too need to make some noise too.

The full schematic and design files are available in a GitHub repository: https://github.com/sdp8483/train_horn

I sell on Tindie

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s