nur sohitHendra Kusumah
Published © GPL3+

Solar Energy-Based AI System For Vehicle Detection And Count

Developed a solar-based AI system capable of detecting and counting the number of vehicles, such as motorcycles and cars, at traffic point

IntermediateFull instructions provided15 days347
Solar Energy-Based AI System For Vehicle Detection And Count

Things used in this project

Hardware components

Seeed Studio XIAO ESP32S3 Sense
Seeed Studio XIAO ESP32S3 Sense
×1
Seeed Studio Vision AI Module V2
×1
Solar Power Manager 5V
DFRobot Solar Power Manager 5V
×1
Panel Surya 6WP 5V 1200mA
×1
Saklar On Off On Waterproof 3 Pin
×2
USB Hub, 4 Port
USB Hub, 4 Port
×1

Software apps and online services

Seeed Studio https://sensecraft.se
google colab

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)
Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free
Plier, Long Nose
Plier, Long Nose
Mastech MS8217 Autorange Digital Multimeter
Digilent Mastech MS8217 Autorange Digital Multimeter

Story

Read more

Custom parts and enclosures

Full

Code

Frimware For Grove AI Vision 2

C/C++
dataset from https://universe.roboflow.com/motorxcar/motorxcar
No preview (download only).

Count CarxMotor

C/C++
#include <Seeed_Arduino_SSCMA.h>
#include <Wire.h>
#include <FS.h>
#include <SD.h>
#include <SPI.h>

// Pin SD card (gunakan pin SD yang sesuai)
#define SD_CS_PIN 21

const int RTC_I2C_ADDRESS = 0x68; // Alamat I2C untuk RTC

const char* days[] = {"Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu"};
const char* months[] = {"Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "November", "Desember"};

byte second = 0;
byte minute = 0;
byte hour = 0;
byte weekday = 0;
byte monthday = 0;
byte month = 0;
byte year = 0;

#define carCountMax 9999
#define motorCountMax 9999

int carCount = 0;
int motorCount = 0;

int xCarBegin = 0;
int xCarEnd = 0;
int xMotorBegin = 0;
int xMotorEnd = 0;

// Loop parameters car
bool loopOccupiedCar = false;
bool loopOccupiedPreviousCar = false;
int loopxMinCar = 20;
int loopxMaxCar = 200;

// Loop parameters motor
bool loopOccupiedMotor = false;
bool loopOccupiedPreviousMotor = false;
int loopxMinMotor = 30;
int loopxMaxMotor = 180;

SSCMA AI;

unsigned long lastWriteTime = 0;
int fileCount = 1;
bool sd_sign = false;

void setup() {
  Wire.begin();
  Serial.begin(115200);
  

  // Inisialisasi SD card
  if (!SD.begin(SD_CS_PIN)) {
    Serial.println("SD Card initialization failed!");
    return;
  }

  uint8_t cardType = SD.cardType();
  if (cardType == CARD_NONE) {
    Serial.println("No SD card attached");
    return;
  }

  sd_sign = true; // SD card initialized successfully
  AI.begin();
 
}

void printTime() {
  char buffer[3];
  const char* AMPM = "";
  membacaWaktu();

  byte displayHour = hour; // Gunakan variabel sementara untuk menampilkan jam

  // Menentukan AM atau PM, dan sesuaikan untuk format 12 jam
  if (displayHour >= 12) {
    if (displayHour > 12) {
      displayHour -= 12;
    }
    AMPM = " PM";
  } else {
    if (displayHour == 0) {
      displayHour = 12; // Jika jam 00, tampilkan 12 AM
    }
    AMPM = " AM";
  }

  // Cetak waktu dalam format yang benar
  Serial.print(days[weekday - 1]);
  Serial.print(" ");
  Serial.print(monthday);
  Serial.print(" ");
  Serial.print(months[month - 1]);
  Serial.print(" 20");
  Serial.print(year);
  Serial.print(" ");
  Serial.print(displayHour);
  Serial.print(":");
  sprintf(buffer, "%02d", minute);
  Serial.print(buffer);
  Serial.print(":");
  sprintf(buffer, "%02d", second);
  Serial.print(buffer);
  Serial.println(AMPM);
}

void membacaWaktu() {
  Wire.beginTransmission(RTC_I2C_ADDRESS);
  Wire.write(byte(0)); 
  Wire.endTransmission();

  Wire.requestFrom(RTC_I2C_ADDRESS, 7); 
  second   = bcdToDec(Wire.read());
  minute   = bcdToDec(Wire.read());
  hour     = bcdToDec(Wire.read());
  weekday  = bcdToDec(Wire.read());
  monthday = bcdToDec(Wire.read());
  month    = bcdToDec(Wire.read());
  year     = bcdToDec(Wire.read());
}

byte bcdToDec(byte val) {
  return ((val / 16 * 10) + (val % 16));
}

void checkAIBox() {
  if (!AI.invoke()) {
    if (AI.boxes().size() > 0) {
      for (int i = 0; i < AI.boxes().size(); i++) {
        int boxX = AI.boxes()[i].x;
        int boxW = AI.boxes()[i].w;
        int xBegin = boxX;
        int xEnd = xBegin + boxW;

        if (boxW > 50) {
          xCarBegin = xBegin;
          xCarEnd = xEnd;

          if ((xCarBegin > loopxMinCar) && (xCarEnd < loopxMaxCar)) {
            loopOccupiedCar = true;
            if (!loopOccupiedPreviousCar) {
              carCount++;
              if (carCount > carCountMax) {
                carCount = carCountMax;
              }
              loopOccupiedPreviousCar = true;
            }
          } else {
            loopOccupiedCar = false;
            loopOccupiedPreviousCar = false;
          }
        } else {
          xMotorBegin = xBegin;
          xMotorEnd = xEnd;

          if ((xMotorBegin > loopxMinMotor) && (xMotorEnd < loopxMaxMotor)) {
            loopOccupiedMotor = true;
            if (!loopOccupiedPreviousMotor) {
              motorCount++;
              if (motorCount > motorCountMax) {
                motorCount = motorCountMax;
              }
              loopOccupiedPreviousMotor = true;
            }
          } else {
            loopOccupiedMotor = false;
            loopOccupiedPreviousMotor = false;
          }
        }
      }
    } else {
      loopOccupiedPreviousCar = false;
      loopOccupiedPreviousMotor = false;
    }
  }
}

void writeFile(fs::FS &fs, const char * path, String data) {
  File file = fs.open(path, FILE_APPEND);
  if (!file) {
    Serial.println("Failed to open file for writing");
    return;
  }
  file.print(data);
  file.close();
}

void loop() {
  checkAIBox();
  printTime();
  Serial.print("CarCount: ");
  Serial.println(carCount);
  Serial.print("MotorCount: ");
  Serial.println(motorCount);

  unsigned long now = millis();
  if ((now - lastWriteTime) >= 1800000) {
    char filename[32];
    sprintf(filename, "/count_data_%02d-%02d-%02d.csv", monthday, month, year);

    String dataString = String(hour) + ":" + String(minute) + ":" + String(second) + ",";
    dataString += "CarCount: " + String(carCount) + ", MotorCount: " + String(motorCount) + "\n";

    writeFile(SD, filename, dataString);
    Serial.printf("Data saved to: %s\r\n", filename);

    lastWriteTime = now;
    carCount = 0;
    motorCount = 0;
    
  }

  delay(500);
}

Credits

nur sohit
1 project • 0 followers
arduino raspberry pi IoT
Hendra Kusumah
36 projects • 141 followers
Love hacking and making new things from IoT to robotics
Thanks to Hendra Kusumah.

Comments