Parking Assistant With A Pic Microcontroller
About the project
A parking assistant that leverages an 8-bit PIC microcontroller to build a parking aid in the form of a traffic light.
Project info
Difficulty: Moderate
Estimated time: 3 days
License: GNU General Public License, version 3 or later (GPL3+)
Items used in this project
Hardware components
View all
Software apps and online services
Hand tools and fabrication machines
Story
Summary
I will be the first to admit (and my wife will undoubtedly agree) that my ability to park our car in our garage is suspect. Pull in too far and I can’t get access to lawn tools. Don’t pull in far enough and the tailgate can’t fully open. A tennis ball on a string may be the solution for some, but is definitely not an acceptable for a self-respecting engineer with an extra PIC controller on his hands.
It has never been easier to get started with embedded electronics projects. Development boards (dev boards) that offer USB “plug-and-play” functionality coupled with robust component-level software libraries means developers can spend more time focusing on the user-facing product features and less time on “under-the-hood” engineering. That translates to two realities that result from lower barriers to entry:
1. It enables products (or projects) that would have not otherwise existed. Think of all the homebrewed Arduino-powered 3D printers that makers are building all around the globe.
2. Small companies and startups can get a functional prototype in front of potential investors faster than ever.
The tradeoff for the convenience of an all-in-one development board is typically cost and potentially footprint size as dev boards are not tailored to your specific requirements. (Although Mouser offers a matrix that compares major features so you can quickly find the open source hardware board that has the critical technologies that you need for your project here: Open Source Hardware Selection Matrix.) Sometimes to get everything exactly as you need it, you have to design it from scratch. There is also something to be said about the satisfaction you get from watching a completely “built from scratch” system come alive for the first time. Learning to design a complete solution from discrete components is an invaluable skill to learn that can pay big dividends in the long-run if you intend to produce a custom circuit board.
To illustrate the different advantages of these two approaches, we are kicking off two projects that will illustrate how different design choices can get you to the same result.
Enter the Digital Parking Assistant
I will be the first to admit (and my wife will undoubtedly agree) that my ability to park our car in our garage is suspect. Pull in too far and I can’t get access to lawn tools. Don’t pull in far enough and the tailgate can’t fully open. A tennis ball on a string may be the solution for some, but is definitely not an acceptable for a self-respecting engineer with an extra PIC controller on his hands.
A parking assistant that leverages an 8-bit PIC microcontroller, an ultrasonic ranging sensor, and a few capacitors, resistors, and LEDs to build a parking aid in the form of a traffic light. Green light letting you know you have plenty of room, yellow meaning to slow it down, and red to indicate that you are at the ideal parking spot. Functional, small and very inexpensive to build.
Materials
We are going to leverage a Microchip 8-bit PIC microcontroller, specifically the PIC18F2520. This microcontroller comes in a very convenient DIP package that can plug straight into your breadboard. Some specification highlights of the 18F2520 includes:
● Up to 10 MIPS performance
● Watchdog Timer with separate RC oscillator
● Wide operating Voltage range: 2.0V to 5.5V
● Idle mode currents down to 5.8uA typical, sleep mode currents down to 0.1uA typical
● 10-bit ADC, 10 channels, 100K samples per second
● Hardware SPI and I2C
● Two PWM outputs
Overview
When building a project using discrete components you have to account for design considerations that you might not otherwise have to contend with if you leverage a pre-assembled development board. Power management and generating a clock signal are two particular aspects of embedded design that have to be considered.
Figure 2: What the circuit will look like on the breadboard. Image created using Fritzing. The actual schematic is shown in Figure 3, of section IV, “System Build Up.”
Power Circuitry
Without the convenience of a micro-USB powered dev board, you have to determine an alternative method to deliver electrons to the project. A 5V “wall wart” power supply is a simple and inexpensive solution. Then it’s just a matter of hooking a barrel jack, a few capacitors and a 7805 voltage regulator to provide the necessary power to the rest of our circuit. This simple circuit will help ensure clean power on the breadboard by filtering out noise and smooth out any voltage sags.
Clock Circuitry
For this project we’re going to demonstrate how use a crystal resonator to setup an external oscillator. In general, crystal oscillators provide some advantages over internal RC timing while still being simple to implement. The timing device is built using a piezoelectric material, commonly quartz, sandwiched between two metal plates. The crystal translates the mechanical resonance into an electrical signal with a set frequency. You can purchase crystal oscillators that produce pulses that range in frequency from a few kilohertz to hundreds of megahertz. We will use an 8MHz crystal for this project. A key advantage of crystals is their very large Q-factor which promotes very good frequency stability. For more information on timing, head over to the blog post Timing is Everything: A Look at Oscillators, Clocks, Buffers and Redrivers.
To determine the value of the two capacitors connected to the crystal, you should check your microcontroller’s data sheet that will provide recommended values for a given crystal frequency. If not available, you can use the following formula as a rule of thumb to determine the capacitor values:
C1, C2 = 2*CL – 2*Cstray
Where Cstray is capacitance induced by the traces and components of the circuit board. CL is the load capacitance of the crystal itself and can be found in the crystal’s datasheet. If in doubt, you can estimate Cstray at 3 pF.
Light Emitting Diodes (LEDs)
Light Emitting Diodes (LEDs) are one of the most fundamental output devices used in open source projects. They provide you a simple way to provide feedback to a user on the status of what’s going on in a circuit. For this project we are using a single red, yellow, and green LED to represent the three colors found in a typical traffic stop light.
Connecting an LED directly to the output pins of the PIC MCU could potentially cause an LED to burn out. You have to utilize a current limiting resistor in series with the LED for protection. In our case we are using 220 Ω resistors. To learn more about how to calculate resistors values for LED applications, check out this blog on calculating LED current for a limiting resistor.
System Build Up
Circuit capture was completed using Mouser’s MultiSim Blue circuit design suite which you can download for free here http://www.mouser.com/multisimblue/
1. Begin by placing the PIC microcontroller IC about ⅓ of the way from one end of the board, with pin 1 facing outward. This will give us sufficient room on either side to build the necessary supporting circuits.
2. Next place the 7805 on the board nearest to pin 1. Then place a 0.22uF electrolytic capacitor across the input terminal and ground. Place a 0.1uF electrolytic capacitor across the output terminal and ground.
3. Place a 10k-ohm resistor between the power rail and pin 1 (/MCLR) of the PIC18F2520.
4. Place a 0.1 μF MLCC capacitor between ground and pin 20 of the PIC. Also connect pin 20 to the 5V power rail.
5. Connect pin 8 to ground.
6. Place the crystal resonator between OSC1 and OSC2 (pins 9 and 10, respectively for the 18F2520). Place a 22 pF capacitor between ground and each of the resonator pins.
7. Cut three extra-long wires so that we can mount the ultrasonic sensor as needed. Two wires are for power and ground, the third wire connect the SIGNAL pin of the ping sensor and pin 25 of the PIC chip.
8. Next place three 220-ohm resistors with one lead each connected to ground.
9. Lastly we connect the PIC chip and the three LEDs. The wires will connect the microcontroller and the anode leads of each LED. Then connect the cathode (the shorter leg) of each LED to the resistors we place in the previous step.
10. To make the long lengths of wire easier to handle and identify, add some heat shrink to each end. Use heat shrink tube of the appropriate color (red, yellow, green) between the LEDs and the input pins of the microcontroller to make wiring up the device less of an issue.
Circuit capture was completed using Mouser’s MultiSim Blue circuit design suite which you can download for free here http://www.mouser.com/multisimblue/
Figure 3: The circuit schematic captured in MultiSim Blue.
Software
The majority of the work done for this project will be in a file named main.c. It will contain three major sections:
IMPORTANT: As of this writing, installing the MPLAB X IDE will not install the necessary compiler. Be sure to download and install the XC8 compiler from this site: http://www.microchip.com/mplab/compilers
Once you have MPLAB X IDE installed, proceed to start a new project. This will launch a wizard that walk you through for setting up a new project structure. Pay careful attention to making sure you select the correct family and device (18F2520), programmer hardware (PICkit 3), and compiler tool chain (XC8).
In this project we will be using Microchip’s MPLAB-X® Integrated Development Environment (IDE) to craft our software in the C programming language. Wikipedia states that “MPLAB is a proprietary freeware integrated development environment for the development of embedded applications on PIC and dsPIC microcontrollers, and is developed by Microchip Technology.” There is a new online version that doesn’t require you to download and install, called MPLAB® Xpress, but we already had MPLAB-X installed, so we used MPLAB-X. To get the code off the computer and onto the microcontroller we use the no-cost MPLAB Integrated Programming Environment (IPE) and a PICkit 3 programmer/debugger tool.
1. Setup
2. Main
3. Support Functions
Figure 4: MPLAB X Integrated Development Environment is a free download for Microchip's website.
Setup
At the top of main.c we will include the necessary header files as well as define various constants. This is very helpful in keeping straight the various mechanisms that PIC microcontrollers use to manage I/O pins. For example:
#define pingUS LATBbits.LATB0 //Latch register, when pin is configured to be an output, this register stores what state the output is in.
#define pingDir TRISBbits.TRISB0 //This register defines whether a pin is an input or output
#define pingIn PORTBbits.RB0 //Read from this register when pin is configured as an input
Instead of trying to remember throughout the code which port and pins support the ultrasonic sensor, we can give them easier to remember names. Now we can change the direction of the pin that connects ultrasonic sensor data line by simply writing the following:
pingIn = 1; //configure as an input
pingIn = 0; //configure as an output
Lastly, there are a few lines of code that begin with #pragma. For our purposes these preprocessor directives tell the programmer how to configure various internal microcontroller control registers. For example, #pragma config WDT = OFF tells microcontroller to turn off the internal watchdog timer.
Main
After a few lines of code that initialize key variables, the majority of the functionality is contained in a while loop that is set to run in perpetuity. In short, we call a function to get the current distance of the car from the parking assistant device. Then based on the distance we make a determination to turn on the correct LED to indicate to the driver if they should stop or if they can proceed.
Support Functions
We’ve created five support functions for this project including:
● void delay_timer(void);
● int checkDistance(void);
● void setGreenLight(void);
● void setYellowLight(void);
● void setRedLight(void);
Programming with PICkit
After the code is written it’s time to get the code off the computer and into the microcontroller. The PICkit programmer connects to your computer via a USB cable. Between the PIC microcontroller and the PICkit we need to hookup five wires. Just match up these pins:
● /MCLR connects to pin 1
● VDD Target connects to pin 20
● VSS Ground connects to pin 8
● PGD connects to pin 28
● PGC connects to pin 27
There is a sixth pin (listed as PGM) that is not required and can be left disconnected.
Next plugin the PICkit to your computer and then launch the MPLAB IPE. Click on Settings > Advanced Mode and enter “microchip” as the password. On the left side click on “Power” and under “ICSP Options” ensure that “Power Target Circuit from Tool” is checked. Navigate back to the “Operate” tab and click on the “Browse” button next to “Source”. This will bring up a dialog box that will allow you to navigate to the .hex file that the MPLAB X IDE generated. Lastly, ensure the correct ‘Family”, “Device” and “Tool” settings are selected from the dropdown menus. Click on “Program” and the IPE software will take care of programming the PIC microcontroller.
Figure 5: MPIPE is the Integrated Programming Environment to get code from a computer to the microcontroller via a PICkit 3 programmer.
Assembly
The plans for the stoplight box have been provided. To mount the device to the wall I took the saw tooth hook off an old picture frame. The device should be placed at a height that allows the ultrasonic sensor to hit the ping signal to reflect a flat part of the car that is perpendicular to the ground, such as the grill or license plate.
If you can’t mount the device near an AC outlet or prefer to eliminate the unsightly wires from the design, consider a battery option. A single 9V battery should provide sufficient power to run the device.
Lastly, I went with a stoplight type enclosure, but feel free to come up with your own design that fits your tastes. Just remember to have the ultrasonic sensor point towards a flat surface that is perpendicular to the ground, as much as possible. You may have to experiment with this for best placement.
Figure 6: A 3D model of the traffic light enclosure.
Leave your feedback...