A Low-Cost Shipment Shock Sensor Using a 6-pin, SOT-23 Microcontroller

Oct. 8, 2008
By Joe Julicher, Microchip Technology Inc. Often, goods are damaged in transit due to mechanical shock or vibration. Unfortunately, the damage is not always visible upon delivery, resulting in disputes between the shipper and its customers

By Joe Julicher, Microchip Technology Inc.

Often, goods are damaged in transit due to mechanical shock or vibration. Unfortunately, the damage is not always visible upon delivery, resulting in disputes between the shipper and its customers that sometimes lead to legal action. With the advent of tiny accelerometers and microcontrollers it is now possible to devise tiny sensors that can be placed in a shipment to detect and measure the presence of damaging vibrations.

Detecting the Vibration The key feature of a vibration sensor is the actual process of detecting a vibration event. This can be done with a minimum of external analog components by taking advantage of an analog comparator that is a standard feature of leading microcontrollers. The advantage of a comparator is that the microcontroller does not need to be awake while the unit is waiting for the event to occur. If an analog-to-digital converter were used, the microcontroller would have the task of repeatedly measuring the vibration and determining whether a vibration event was occurring.

The most common accelerometers produce a voltage proportional to the G-forces applied to the sensor. The accelerometer signal is generally a DC voltage with a 0 G voltage of Vdd/2. Unfortunately, the 0 G voltage typically drifts with age and temperature. If the drift gets too large, the voltage could trip the comparator and cause a fault vibration trigger. The solution is a combination of a low-pass and a high-pass filter to focus on the desired vibration signals (see Figure 1).

The circuit shown above uses a conventional comparator to demonstrate the concept of the vibration detector. The low-pass filter uses the internal resistance of the accelerometer and capacitor C3. This has a 3 db point of approximately 5 kHz. The second filter consists of C1 and R1, R2 and has a 3 db point of approximately 500 Hz. Any vibration with frequencies between 500 and 5 kHz will cause the comparator to trip. R1+R2 should equal the 32 kOhms, because the internal resistance of the accelerometer is 32 kOhms. R1 and R2 allow the acceleration signal to be attenuated to fine tune the G force detection threshold. The output of this comparator will now toggle with vibration. The comparator is comparing the vibration signal with ground. This is a simplification that works because most comparators are not sensitive all the way to ground and will require a few millivolts of signal to detect the signal.

Choosing a Microcontroller After the signal has been sliced and compared, the microcontroller must process the data and determine the severity of the vibration. There are many microcontrollers that would fit in this application. But, an ideal choice would be one that is very low power, very low cost and very small. The 6-pin, SOT-23 PIC10F204 microcontroller fits this description very well. The PIC10F204 has an internal comparator, so we will not require an external comparator. After the detection circuit is attached to the PIC10F204, there are two pins remaining for a simple user interface.
Example Design With the microcontroller chosen and the analog circuits designed, all that is left is to combine our circuits, layout the board and write the software. In this simple example, an LED is used to report the presence of vibrations. A switch is provided to allow a simple user interface (see Figure 3). This entire circuit lays out on top of the battery with a two-layer board.
Software There are many choices for vibration sensing. To make this example very simple, we will program the microcontroller to go to sleep and wait for shocks. When the shocks come, the microcontroller will wake up, count the shock and return to sleep. A button press will wake the microcontroller and cause the LED to blink out the shock count. When the blinking is complete, the shock counter is cleared.
	list      p=10F204            ; list directive to define processor
	#include         ; processor specific variable definitions

	; configured for no watchdog timer, no master clear and internal RC
shocks          EQU     0x07    ; shock counter
GPIO_PV			EQU		0x08	; previous value of GPIO
temp			EQU		0x09	; working variable
PIN_CHANGED		EQU		0x0A	; the changed pins
delay_cntr0		EQU		0x0B	; first delay counter
delay_cntr1		EQU		0x0C	; second delay counter
	org	0x0ff
;	movlw	0xA5	; dummy osccal value
	org 0x00
	movwf	OSCCAL	; calibrate the oscillator
	MOVLW	0X09	; configure the PORT for I/O's
	TRIS	GPIO	; GP0 is Input, GP1 is Output
					; GP2 is Output, GP3 is Input
; the PIC10F200 wakes up from sleep by going through reset.
; however it can determine the reset source.
; therefore the first step is to learn the cause of the reset.
	goto	wakeup_pin_change

	clrf	STATUS
	clrf	shocks
	movf	GPIO,w
	movwf	GPIO_PV
	movf	GPIO,w
	movwf	temp
	xorwf	GPIO_PV,w
	movwf	PIN_CHANGED	; PIN_CHANGED holds all the chnaged pins
	andwf	temp,f		; temp holds the changed pins that went high
	btfsc	temp,3		; did the button get released?
	sleep			; don't worry about released buttons
	btfsc	PIN_CHANGED,3	; did the button get pressed?
	goto	button_pressed ; handle the button press
	incf	shocks,f	; it must be a vibration...count it
	sleep			; go back to sleep

	movf	shocks,w	; test to see if shock counter is clear
	btfsc	STATUS,Z
	goto	no_shocks_left ; if it is clear then we wait for the button to be pressed
; it is time to blink the led the total number of shocks
	bsf	GPIO,2		; LED ON
	call	quarter_second_delay ; wait for 1/4 second
	bcf	GPIO,2  	; LED OFF
	call	quarter_second_delay ; wait for 1/4 second
	decf	shocks,f	; decrement the shock count
	goto	button_pressed	; repeat
	btfss	GPIO,3
	goto	no_shocks_left	; wait here until button is released
	movf	GPIO,w		; reload a fresh previous value
	movwf	GPIO_PV		; save it for the wakeup

	clrf	delay_cntr0
	movlw	0xC3
	movwf	delay_cntr1
	goto	$+1
	decfsz	delay_cntr0,f
	goto	delay_loop
	decfsz	delay_cntr1,f
	goto	delay_loop
	retlw	0

The software above is very simple and only counts shocks. If the actual duration of a shock needed to be tested, then the microcontroller could easily stay awake for a few hundred milliseconds to see if the shocks continue. If they continue then the duration can be measured. If they do not continue, then the microcontroller can go back to sleep. When the microcontroller is sleeping the instruction clock is turned off. With the clock off, there is no way to determine how far apart the shocks are. Measurements become a tradeoff between the minimum power of sleep and the most accurate measurements.

Conclusions Small mechanical sensors, based on springs and weights, exist that provide a visual indication of shock. However, they are only one-time use, and they provide no information concerning the number or duration of the shock events sustained by the package.

This article describes a small electronic watchdog circuit that can be reused repeatedly, provides a visual indication of shock events, and counts the number of shocks that have occurred. The watchdog circuit uses small, low-cost 2-axis G sensors and a 6-pin, SOT-23 microcontroller to monitor the vibration and shock sustained by the unit. By proper design of the signal conditioning circuitry, the acceleration due to movement can be filtered. This allows the circuit to detect and record the duration of the damaging shock. With additional software and other enhancements this device can be built into a complete data collection system for a shipping company. As is, this device counts how many times your fragile products are being mistreated while en route to their destinations.

Joe Julicher is a Principal Applications Engineer for the Security, Microcontroller and Technology Development Division, Microchip Technology Inc.


Product URL: Click here for more information


To join the conversation, and become an exclusive member of Electronic Design, create an account today!