Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Tyler HayesReece Walser
Published

Upgraded RC Car | MEGR 3171 IoT Project

An RC Car equipped with an impact sensor and ultrasonic distance sensor.

IntermediateFull instructions provided1 hour123
Upgraded RC Car | MEGR 3171 IoT Project

Things used in this project

Hardware components

An RC Car
×1
Photon 2
Particle Photon 2
×2
Impact Switch Sensor
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE
ThingSpeak API
ThingSpeak API

Story

Read more

Schematics

Overall Circuit Diagram

Shows both Photon 2s with their respective sensors.

Impact Switch Circuit Diagram

Shows the configuration of the Impact Switch sensor and a Photon 2.

HC-SRO4 Circuit Diagram

Shows the configuration of the HC-SRO4 sensor and a Photon 2.

Code

HC-SRO4 Ultrasonic Distance Sensor Code

C/C++
ChatGPT was a huge help in creating this code.
// Define the pins for the sensor
const int trigPin = A0;
const int echoPin = D0;

// Variables to store the distance and the publish interval
int distance = 0;
unsigned long lastPublishTime = 0;
const int publishInterval = 1000; // Publish every 1 second

SerialLogHandler logHandler;
int i = 0;

void myHandler(const char *event, const char *data)
{
  i++;
  Log.info("%d: event=%s data=%s", i, event, (data ? data : "NULL"));
}

void setup() {
  // Initialize serial communication
  Serial.begin(9600);

  // Initialize the sensor pins
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
  Particle.subscribe("Impact", myHandler);
}

void loop() {
  // Trigger the sensor
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the echo pin to calculate distance
  long duration = pulseIn(echoPin, HIGH);
  distance = microsecondsToInches(duration);

  // Print the distance to the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" inches");

  // Publish the distance every publishInterval milliseconds
  if (millis() - lastPublishTime > publishInterval) {
    Particle.publish("distance", String(distance), PRIVATE);
    lastPublishTime = millis();
  }

  // Add a delay to avoid spamming the cloud with too many publishes
  delay(1000);
}

// Function to convert microseconds to inches
long microsecondsToInches(long microseconds) {
  // The speed of sound is 1130 feet per second or 1 foot per 29.1 microseconds
  // The HC-SR04 measures the time it takes for the sound wave to return
  // and converts it to distance using the speed of sound
  return microseconds / 74 / 2;
}

Impact Switch Sensor Code

C/C++
int impactpin = D6;
bool impact = 0;

SerialLogHandler logHandler;
int i = 0;

void myHandler(const char *event, const char *data)
{
  i++;
  Log.info("%d: event=%s data=%s", i, event, (data ? data : "NULL"));
}

void setup() {
    
    pinMode(impactpin, INPUT);
    
    Particle.subscribe("distance", myHandler);
}

void loop() {
    
    impact = digitalRead(impactpin);
    Particle.publish("Impact", String(impact));
    
    delay(1000);
}

Credits

Tyler Hayes

Tyler Hayes

1 project • 1 follower
Reece Walser

Reece Walser

1 project • 1 follower

Comments