You Are Here: Home » AVR ATmega Projects » Security - Safety Projects » Ultrasonic Security System Using Atmega644

Ultrasonic Security System Using Atmega644


We were inspired to build an ultrasonic security system for our final project by our housing situation this summer. Security is an important part of home, especially if we are going to share a house with prior strangers without a lock on our room door. Yes, that is the situation we are walking into in order to drive down the cost of living in NYC. And we anticipate that many college students could face a similar problem. What is not to love about a device that looks like WALL-E and scans around for possible intruders? In case of intruders, it sends off a sound alarm and alerts the owner via email. It is also password protected and could be disabled via the correct password.

Ultrasonic Security System Using Atmega644

High Level Design

Rationale for project idea

We came up with the idea because it could help us and others protect property and privacy. Traditional household security systems often require installation and detect based on opening of doors and windows. In the cases where installation is not possible and/or the area of interest has no door, our ultrasonic security system will come in handy because it requires no installation, and detects intruders based on their physical presence.


The system uses ultrasonic sensor that has a transmitter part and a receiver part. The ultrasonic transmitter periodically emits ultrasonic signals into an open area in front of it. To cover a wide range, a rotating servomotor is used to allow the sensor (transmitter and receiver pair) to cover roughly 180 degrees. If the signal ever hits a physical object, it will be reflected back and, the receiver part of the sensor will then capture it with the object considered detected as its position (distance from the device and angle relative to the device) is now known. As the security system sends off a sound alarm, the object position is polled by our MATLAB script, which then plots it on a graph and log the record. The record log is also sent as an email to an address set by the owner in advance.

From the user’s perspective, here is the flow of the system: LCD indicates user may press the button “A” to start. After user presses “A”, the LCD further asks user to set up a password that could be used to disable the security system later, followed by the button “#”. After the user finishes entering the password, the password is stored for the session, and the security system becomes active: motor starts rotating, sensor is scanning for potential intruders within a distance (15 cm for our demo, but could be anything realistically. It is only constrained by the sensor range, which is 5 meters theoretically), and in the case that an object is detected, the system beeps and triggers MATLAB to send an email containing the time of detection to the user. If the user wishes to disable the security system, he or she could type in the password used to set up the session. LCD will then indicate session is ending, and a new session is started in 2 seconds.

Logical structure

We will first talk about our hardware logical structure. The key hardware for this project are Atmega644 microcontroller, URM37v3.2 ultrasonic sensor, Parallax servo, LCD, and a keypad. The microcontroller sends the LCD various message depending on the system state. Also, a user is expected to enter a password each time he/she begins and terminates the system through the keypad. Finally, the microcontroller communicates with both the sensor and PC (MATLAB) via same rs232 serial port. Therefore, the sensor and MATLAB distinguishes the commands by using unique headers.

Now to the software logical structure. There are three functions that our C code runs periodically: debounce function that debounces keypad in order to read user input correctly, the system state function that moves through system stages (waiting to password to on, on to OFF, setting up passwords, etc.), and the active security function that rotates servomotor and sends signal off to detect potential objects. We will describe all three functions in more details under the Software Design section. These tasks go off at different times as they each have their own countdowns, decremented and reset accordingly when a timer interrupt is received. Once their times are up, the tasks are called by the while(1) loop in main. Debounce task sets a flag when user presses keys, this flag helps the second task to move system to the next state and operate accordingly. The second task also sets a flag when the system state is set to ON, and this flag is needed for the third task because we only rotate our motor and look out for objects/intruders when the security system is ON (owner has set up password and system is ready to go). When an object is detected, a trigger is sent to our MATLAB script which has been polling for an intruder trigger. Once the MATLAB script is triggered, it plots the object’s position and sends user an emai

IEEE and FCC regulations

Hardware Design

The Atmega644 microcontroller was used to interface with the URM37v3.2 ultrasonic sensor via rs232, Parallax standard servo motor via PWM, and to the MATLAB running on a PC via rs232. For user interactions, there are LCD and keypad wired to PORTC and PORTA respectively of the microcontroller. We will discuss how each of these hardware interfaces with the microcontroller.

Atmega644 microcontroller

As the brain for the project, we built a target board for Atmega644 chip. The target board itself was designed by Bruce Land, and the layout is as follows (images taken from course website):

The key features of Atmega644 microcontroller that we used for this project are the three timers and the serial rs232 USART. The timers are responsible of scheduling periodical execution of various tasks (refer to “Software”), generate alarm tone, and generate PWM signal with appropriate duty cycle and frequency to control the servo motor.

The serial USART is used to communicate with the ultrasonic sensor and with the MATLAB running on a PC.

The one problem we faced was the fact that there is only one USART, and there are two items trying to connect through it. Thus, both the sensor and the MATLAB will see all the outgoing bytes. We resolved the issue by simply wrapping the data with unique header. It turns out that the sensor requires the header to be 0x22 in order initiate a distance measurement (more on this later), and therefore we assigned 0x01 for MATLAB. Thus, both MATLAB and sensor will be constantly polling for a command, but will only act upon it if the header matches.

URM37v3.2 ultrasonic sensor

The URM37v3.2 sensor was purchased through DFRobot ( It has its own little avr processor which executes a distance measurement when commanded by the main MCU (atmega644). Here are the front and back layouts of it:

The sensor has multiple modes of operation such as serial passive control mode which always waits for command to initiate a measurement, autonomous trigger mode which makes measures every 25msec, and PWM passive control mode which outputs the measurement in an analog PWM format. We have decided to keep the sensor in the passive control mode for simplicity. The specific command that the sensor awaits to measure distance is: {0x22, degree, 0x00, checksum}. The command is sent via rs232 serial communication with 9600 baud, 8 bit data, no parity, and 1 stop bit (8N1). The degree mentioned above is an optional feature of the sensor to directly control the servo motor to that specific angle. In our case, the main atmega644 is responsible of the servo motor position, and thus the command that is sent is: {0x22, 0x00, 0x00, 0x22}.

Ultrasonic Security System Using Atmega644 Schemetic

When the sensor receives this particular command, it responds with {0x22, high distance, low distance, checksum}. Thus, the measurement can be retrieved by shifting the high distance and or-ing with the low distance: dist = (high_dist<<8) | low_dist.

Parallax standard servo motor

The servo motor requires a 5v dc power supply, and is controlled via PWM signal of roughly 50Hz. The position of the servo is determined by the duty cycle of the signal.

The servo motor can produce inductive spikes, and therefore we placed an opto-isolator between the MCU and the motor to protect the chips.

Parts List:

For more detail: Ultrasonic Security System Using Atmega644

Leave a Comment

You must be logged in to post a comment.

Read previous post:
Smart Trash system Using Atmega644

Introduction top For our final project, we have designed and built a 'proof of concept' prototype for Project Green Stations,...

Scroll to top