DIY traffic light with PIC microcontroller.

Open source hardware project.

Features:
  • Automatic cycle of lights.
  • Super simple boost converter driven from PIC microcontroller for LEDs supply.
  • Easy construction.
  • Lights speed adjustable by potentiometer
  • Powered by 4xAAA or AA batteries. In theory can also be powered by LiPol cell.
  • Very small current consumption. Long battery life.
  • Push button for power ON/OFF and manual advance of lights.
  • Controlled by PIC16F1824 microcontroller
  • Fun thing to play with
The circuit:

 Traffic light circuit

Background:

This project was inspired by my daughters kindergarten. One day they showed up with some electronic parts including 555 timer and one led for each color and wanted a traffic light for the kids game. I was not happy with that design, and wanted some more power, so I designed a completely new circuit and printed circuit boards. Of cource with a PIC microcontroller. Fortuantely I found out at the time that it is possible to order the PCB-s of 3 different colors just suitable for traffic light. So I managed to come up with very nice circuit and overall design, that I am happy with. And hopefully children are also happy with it.

Technical description:

PIC microcontroller generates PWM signal for boost converter circuit (Q1;L1;D3). It is mainly constant frequency and constant duty cycle. Software only corrects the duty cycle a little for different lights so that all lights seem equal intencity. The 36V stabilitron D4 is important component for safety, it clamps the maximum voltage to 36V in case the leds are disconnected.  The boost converter for LEDs do not need much output capacitance because LED-s are not sensitive to current fluctuations. Also the output current is limited by inductor current, so all suits very well.

Q1, Q2 and Q3 are used for switching between the lights. Microcontroller again takes care of the timing.

RV1 potentiometer is used for adjusting the lights cycle speed. Microcontroller reads the potentiometer position and adjusts the cycle duration accordingly.

Circuit as a pdf here:trafficlight.pdf

Firmware:

The latest code uses the state machine approach for easily managing the lights sequence. I actually had to measure the real traffic light changing speed from the video made on the cross section. Interesting thing is most people do not know that for a period of time the red and yellow lights are both on before the green appears! Even those who have driven in the city for years!

PWM signal is generated by the PIC built is PWM module so that is really easy too. Only configure the parameters and go! PWM values are fixed values found out during testing so no difficult feedback system there. All plain and simple.

The one button is coded as follows:

  • Short push (when off) -- Power on. Actually wake up from sleep
  • Short push (when on) -- Advance to the next color manually.
  • Long push (hold button for little longer) -- Power off. The microcontroller is in sleep during OFF state. All other things turned off.

Firmware assembler source code here:mainfoor.asm

Compiled hex file for directly programming into PIC16F1824 here:valgusfoor.hex(rename downloaded the file -- remove .txt from the end)

Originally compiled under the MPLAB IDE v.8.92 but also MPLAB X should work very well.

Note that in the circuit diagram it shows PIC16F1823 but this is repaced with newer PIC16F1824 in the final circuit!

And yes the firmware is quite state of the art and maybe a little overkill for so simple application but I had a good time coding and recoding it ;) Eventually I am happy with it!

 Printed Circuit Boards:
3D thumb


I made the layout in the Kicad Pcbnew and ordered the three different color boards from the PCB factory -- Red,Yellow,Green. This way I got exactly one mainboard for every 3 light boards. Also the mainboards are in 3 different colors to choose from.

 Board in KiCad

Here is how the board looks in the Kicad Pcbnew.

The original Kicad files here (and also Gerber files for ordering PCB-s from PCB manufacturer):Trafficlight_Kicad+gerber.zip

The boards need to be cut open with saw. Could be made with V-groove but I did not use it because the V-groove panelizing is at extra cost for the prototype PCBs. So design is originally for cutting the boards with hacksaw (later I turned to small narrow kerf circular saw).

PCB boards and kit of parts also available in my shop.

 Enclosure:

Enclosure is most easy to make form empty carton juice containter. Holes cut and boards glued in with hot glue gun. All covered with black or gray carton. Sun shades also forom carton glued to the lights. Hatch made to the back to access the boards and the batteries. With pin headers soldered to the boards the components are then easily wired together with jumper wires.

DIY Metal detector project with PIC12F1840 microcontroller.

 

This is open hardware DIY project. It is possible to make either pinpointer or a full size metal detector based on this circuit.

Figure 1. This is the circuit:

Metal detector schematic

Features:

  • Very easy to build.
  • Search coil is simple monocoil with no adjustment needed. Just use standard inductor or wind some turns of wire yourself and start detecting.
  • Moving mode detecting, search algorithm automatically adapts to the coil parameters and detects as those change in time. So there is no manual tuning.
  • Discrimination between different metals. Non-ferrous result in lower tone than ferrous metals.
  • Led "power on" indication.
  • Push button ON/OFF switch
  • Low power consumption 20-50 mA
  • Runs directly from 4 NiMH cells
  • Basic discrimination features. Gold makes slightly different sound from iron.
  • In circuit programming (ICSP) connector for easy firmware uploading. NEW!

  I keep pursuing my original goal, to keep the schematic as simple as possible with as few components as possible, yet to have good sensitivity. Reason I publish all details is that I want others also benefit from the hard work I have already done.

Design files:

Version 3.00:

Quick link to the PIC12F1840 datasheet:41441B.pdf

Firmware source code version 1.80: firmware180.zip;   HEX file for PIC:metaldetector_hex.zip

Coil:

This circuit has been tested to work with different coils. Software algorithm automatically adapts to the coil parameters.

Basic coil is 20cm diameter, and 27 turns of 0,74mm2 copper electrical installation wire. Regular 0,5mm or less diameter insulated solid copper coil wire is also good. There are many good coil making instructions in the internet.

Coil inductance in the circuit is given as a guide only. You can use variety of coils with different inductances. Circuit must still work. Possibly the reasonable range is 150μH to 470μH. Coil resistance in the range of 0,25 to 2 ohms.

For pinpointer it is preferred to use off the shelf ferrite core inductor for the coil. I've found that 470μH and 1,8A 0,28Ω rating works fine.

Background:

One day I decided that I need a metal detector. Motivation to that came from repeatedly sawing into hidden metal inside wood with my chainsaw mill, and ruining my saw chain. So logical step was to get a metal detector. Then I researched market for metal detectors. And of course found out that cheap ones are probably crap and better ones I can´t afford. Then searched web for DIY metal detectors. I soon realised that all available circuits are not for me. Well, microcontrollers have been around now forever and those small ones are so cheap and relatively powerful. So why bother building some ancient design metal detector with several opamps and bunch of resistors and other components. Surely we can do better nowadays -- We can do it with one 8 pin PIC microcontroller and very few external components! I guess I make separate article about my sawmilling system some day.

So here is, how to build build good metal detector for only microcontroller cost, all other components and coil wire can be obtained from electronics crap laying around everywhere, and if you want to program PIC yourself, you need some programmer device compatible with PIC12F1840. I personally use PICKIT3. I bought PICKIT3 because sadly i discovered that PIC12F1840 was not supported by my JDM and Parallel TAIT programmers. If you do not have programmer you can purchase preprogrammed microcontroller from my shop.

Technical description:

I call this “Pulse oscillation decay” type detector or just "Pulse oscillation" detector. In principe it is inspired by commonly known pulse induction detectors. Current pulse is sent to the coil and then response is measured. In my detector circuit the coil is not dumped by dumping resistor as found in common pulse induction detectors. High current pulse is applied to the coil and after the pulse is cut off, oscillation occurs in a tank circuit formed by search coil and capacitor in parallel with it. This oscillation is by the way relatively HIGH VOLTAGE. So all circuit must be well isolated to avoid electric shock! Oscillation is then of course decaying fast, because of losses, and because energy supply to the circuit is cut off. Mainly there are constant resistive losses in oscillator circuit, and apart from that there are EDDY CURRENT losses in possible metal target. Microcontroller just have to measure the decay time, to detect differences in oscillator circuit losses. And by all means, if resistive losses are constant, any other decay time change means there is METAL TARGET near the coil.

Coil oscillation frequency is roughly set by coil inductance and parallel capacitor value. And frequency also changes slightly depending of the target metal properties. The ferromagnetic metal target objects decrease free oscillation frequency and non-magnetic metals increase oscillation frequency. So it is even possible to discriminate between targets with this method, and this function is included in latest firmware.

Oscillation maximum voltage is also dependent of the C1 value. Capacitor C1 is chosen so that voltage at the coil never exceeds about 150v, the MOSFETs rated voltage. Mosfet I use in latest working rig is IRLI630. Most logic level drive and 150V mosfets should work. Mosfet avalanche must be avoided, it is possibly not very stable woking region. Higher voltage mosfets have always larger on-state resistance which in turn limits maximum current for given supply voltage. It is reasonable to choose 200V maximum voltage mosfet transistor, if supply voltage is 4,8V from 4 NiMH cells.

 

Figure 2. Search coil one pulse voltage.

coil voltage

 

In my design pulses occur at 2 millisecond intervals. Pulse duration is 140 microseconds. Pulse timing is taken care by PIC microcontroller and MOSFET is directly driven by PIC output pin trough R3. Coil Pulse current is limited only by MOSFET on-state resistance and search coil resistance. This makes pulse current as high as possible – more sensitivity. At the same time, as pulses are very short, circuit average current consumption is very low – no need to carry huge batteries with you.
Use ONLY 4 NiMH or NiCd cells to supply this circuit! There is no supply voltage limiting circuit and four Alkaline batteries voltage will be 6V, which is too high for PIC microcontroller!

I repeat: This circuit is designed to use 4 NIMH cells(AA or AAA) in series for power supply.

 

 Figure 3. Receiver side block diagram:

 This is equivalent circuit about what  is happening inside PIC12F1840. The PIC internal features are configured in the way shown. Input pin is configured to comparator -input, comparator +input is internally connected to Digital to Analog Converter which supplies reference, 32 voltage levels between V+ and V- possible. Comparator output is internally connected to TIMER1 gate. This valuable function only lets the Timer1 to count when comparator output is high. Program then activates Timer1 just after the coil pulse is ended and reads the value from the timer before starting the new pulse. And this is our measurement. Timer1 runs at system frequency of 32MHz and so has resolution of 31.25nsec.

 Of course we can not let the high voltage signal reach the microcontroller. This is why there is limiting circuit of R4,D2,D3. Schottky diodes D2 and D3 dump the excessive voltage to supply rails. So voltage reaching PIC input is always in the range of supply voltage. Diodes D2 and D3 must be Schottky type, regular diodes are not fast enough and microcontroller likely gets damaged. To be exact I also tried the circuit without the diodes D1 and D2 and it seemed to work well because of the PIC internalt protection diodes, but there is too few testing to reccommend that.

 

Figure 4. Limited voltage waveform at Microcontroller input.

Voltage at PIC input

 Notice how upper part of the oscillation is almost completely limited out, and lower part is limited to negative supply V-. Oscillation center point is positive supply V+.

Firmware:

PIC 12F1840 firmware is now written in assembler using MPLAB X environment. When I started the project I used  MPLAB IDE v8.83.

Firmware pushes this little microcontroller to its limits regarding speed and takes full advantage of the PIC´s on-board peripherals. Using PIC microcontroller superior power management capabilities it made possible to eliminate physical power switch form circuit. All functions are controlled only by one push button. When circuit is turned off PIC is in sleep and current draw is virtually none. Much less than NiMH batteries self discharge anyway.

Sound generator just uses timer2 to toggle speaker outputs. Speaker is connected between two outputs because this creates sort of bridge circuit, voltage is doubled, sound is stronger, and signal do not have DC offset.

PIC resources used:Interrupts, Interrupt-on-change, Sleep mode,DAC, Comparator, all Timers(Timer0,Timer1,Timer2).

Active version 1.79:

Features:

  • Slight discrimination between different metals. The non-ferrous metals make lower tone than ferrous metal(iron).
  • Single button push turns detector on.
  • Double push on the button changes operating mode.There are 4 modes:
  1. discriminating mode, standard on/off sound
  2. gradual beeping(distance indication), now with discrimination
  3. discriminating mode, less filtered than mode1
  4. silent mode, only LED on/off
  • Longer push on the button turns detector off

Firmware source code version 1.80: firmware180.zip  HEX file for PIC:HEX file


Older versions (no discrimination yet):

version 1.25 with RS232 (COM port) communication(10.01.2013) assembler source code:PO_metaldetector125.zip ver1.25 hex file:metaldetector125.hex

Button double push activates the serial rs232 sending, single push returns to normal mode, longer push turns power off.

ver1.10(22.12.2012):metaldetector110.asm ver1.10 hex file:metaldetector110.hex This is not recommended version, but I leave it here for now.

Ver 1.00 :metaldetector100.asm  Ver1.00 hex file :metaldetector100.hex  most basic version but works. No discrimination.

Printed circuit board (PCB):

Currently there is three versions of PCB-s designed.

V1.00 First version was one sided board.


V2.00 version was two sided board (actually by design may work with only bottom layer) but when I designed second version I converted to newer Kicad version and redraw the schematic, and did accidentally swap the LED connection and piezo speaker connection to PIC pins (5;6). So  ver.2.00 need slightly modified firmware. But I already managed to order bunch of those boards from the PCB factory. Non of those boards shipped from my shop.


V3.00 is two sided board. Compatible with version 1.00 with following improvements:

  • C2 and Q1 mounted horizontally to enable mounting the circuit inside the tube.
  • now has mounting holes in the corners
  • switch mounting in the center position, to enable better button shaft mounting
  • ICSP connector

I recommend that PIC should be socketed, although ver.3 board have "In Circuit Serial Programming"(ICSP) possibility. PCB dimensions 30 x 60 mm.

PCBs and kits of parts are also available in my shop.

Links:

Discussion and information backup in the forums:

http://www.geotech1.com/forums/showthread.php?19935-My-Pulse-Oscillation-detector-project

http://www.thunting.com/smf/metal_detectors/diy_pic12f1840_microcontroller_based_metal_detector_project-t38721.0.html

for general electronics and microcontrollers:http://www.eevblog.com/forum/

Thanks for everybody who participates in those forums, it has helped me a lot to go forward with this project.

Now I hope this site contains all needed information to sucessfully build working metal detector.

E-mail me if you have any comments or questions: This email address is being protected from spambots. You need JavaScript enabled to view it.

 

Subcategories