Head office, Rana Pratap Bagh, Delhi011 2721 1512+91 97116 99511

Science project idea

Traffic signal controller in register-level C on an ATmega328P with a pedestrian request

Arduino code hides what a microcontroller does. Writing the program straight to the ATmega328P registers shows how ports, timers and interrupts work. The project builds a four-way traffic signal model with a state machine, timer interrupts and a pedestrian button, and measures timing accuracy.

What it shows

Arduino code hides what a microcontroller does. Writing the program straight to the ATmega328P registers shows how ports, timers and interrupts work. The project builds a four-way traffic signal model with a state machine, timer interrupts and a pedestrian button, and measures timing accuracy.

The science

Data direction registers (DDRx) set a pin as output or input, and PORTx writes its level. Timer1 in CTC mode counts clock ticks up to a compare value and raises an interrupt at exactly one second with a suitable prescaler (16 MHz / 1024 gives 15625 ticks per second). An interrupt service routine updates a state machine, so the main loop can handle the button. External interrupt INT0 detects the pedestrian request without polling.

Materials

Cost: under Rs 1,500. Many items come from home or from waste; spare parts such as LEDs, motors and wire are in the store.

  • An ATmega328P chip on an Arduino Uno board or a bare chip with a 16 MHz crystal and 22 pF capacitors
  • An AVR programmer or the Uno bootloader and avr-gcc with avrdude (or Microchip Studio)
  • 12 LEDs (red, yellow, green for four arms) with 330 Ω resistors
  • A push button for pedestrians, a buzzer
  • A breadboard or a veroboard
  • A logic analyser or a phone oscilloscope for checking times (optional)

Safety

For this project: Use USB power of 5 V. Check the LED resistor values so that the pin current stays below 20 mA. An adult must supervise soldering.

Never use mains electricity: power models with cells, batteries or a low-voltage adapter. Sharp and hot tools, flames and lab chemicals need an adult or your teacher beside you. Keep magnets, button cells and small parts away from young children, and wash your hands after making.

All the safety rules

Step by step

11 steps. Do them in order, and test each part before you go on to the next. Write what you notice in a notebook as you go.

  1. Draw the intersection and list the states: north-south green, yellow, all red, east-west green, yellow, all red, with their times.

  2. Draw the state diagram and the table that gives LED patterns for each state.

  3. Map the 12 LEDs to ports B, C and D and write the DDR setup code.

  4. Configure Timer1 in CTC mode with a prescaler of 1024 and OCR1A = 15624 for a one-second interrupt.

  5. Write the ISR that decrements a counter and moves to the next state when it reaches zero.

  6. Define the state table as a constant array in flash and use it to set the port outputs.

  7. Configure INT0 on the falling edge for the pedestrian button with a software debounce of 50 ms using the timer.

  8. On a request, finish the current green safely and then show a pedestrian phase with the buzzer.

  9. Measure the cycle with a stopwatch over ten cycles and with the logic analyser if available, and calculate the error in parts per million.

  10. Compare the code size in bytes with an equivalent Arduino-library version.

  11. Write the report with the state diagram, register setup explanation, timing test and a safety discussion of fail-safe traffic lights.

What you should see

The model runs the cycle with timing accurate to within a fraction of a percent, handles pedestrian requests without missing any, and the compiled program is much smaller than a library-based version.

Viva questions

A teacher or a judge will ask how it works. Try to answer each question before you open it, then compare.

1What does DDRB do?

It sets each pin of port B as an input (0) or an output (1).

2How is a one-second interrupt created?

Timer1 counts 16 MHz divided by 1024, and a compare match every 15625 counts raises an interrupt.

3What is switch bounce?

The contact makes and breaks several times in milliseconds when pressed, causing false multiple triggers.

4Why use a state machine?

It makes the sequence clear, easy to test and prevents unsafe combinations like two green lights at once.

Free ebook: Viva Questions for Science Projects

Take it further

Add a sensor-based mode that extends green time when a vehicle detector sees a queue, using an IR sensor model.

Change one thing, measure what happens, and add a table or chart of your readings: that is what turns a model into your own investigation.

Free ebooks for this project

WhatsApp us