Introduction: Easiest Traffic Counter Using Arduino

By: Kaveena & Seshu

Step 1: Materials

  • Arduino
  • Power Cord (for Arduino)
  • Breadboard
  • Ultrasonic sensor
  • Male to Male wires (4)

Step 2: Circuit Diagram

Complete the circuit diagram as follows.

Step 3: Copy Code

Copy and paste the code into your Arduino program:

//source example: mechatronics

// defines pins numbers
const int trigPin = 9; const int echoPin = 10;

// defines variables long duration;

int distance;

float counter = 0;

void setup() { pinMode(trigPin, OUTPUT);

// Sets the trigPin as an Output pinMode(echoPin, INPUT);

// Sets the echoPin as an Input Serial.begin(9600);

// Starts the serial communication }

void loop() { // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH);


digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH);

// Calculating the distance distance= duration*0.034/2;

// Prints the distance on the Serial Monitor Serial.print("Distance: "); Serial.println(distance); delay(900);

if (distance < 200 && distance > 10){ counter = counter + 1; Serial.print("Cars: ");





Step 4: Set Up & Monitor Traffic

Go outside and turn on the serial monitor to view the counter.

Step 5: Next Steps:

If you want your traffic counter to live outside and display data online, use a raspberry pi instead. Stay tuned for our next installment that will show you how!