The article titled “Smart Conveyor System with Automatic Object Sorting using Favoriot IoT Platform” provides a step-by-step guide to building a smart conveyor system integrated with the Favoriot IoT Platform for remote monitoring.


Step 1: Hardware Preparation

Main Components

  1. Microcontroller: ESP32 or ESP8266
  2. Sensors for Object Detection:
    • IR Sensor: Detects the presence of objects
    • Color Sensor TCS3200/TCS230: Detects the color of objects
    • Ultrasonic Sensor HC-SR04: Measures the size of objects
  3. Servo Motor: Used to push objects into their respective categories
  4. DC Motor with L298N Driver: Drives the conveyor belt
  5. Power Supply: 5V or 12V, depending on component requirements

Step 2: Hardware Connections

  1. DC Motor: Connect to the L298N Driver to control conveyor movement.
  2. IR Sensor: Install at the end of the conveyor to detect the presence of objects.
  3. Color Sensor TCS3200: Connect to the microcontroller to read object colors.
  4. Servo Motor: Connect to the microcontroller to push objects into designated categories.

Step 3: Code for Color Detection & Sorting

The code utilizes the TCS3200 color sensor to read object colors, operates the servo motor to sort objects based on color, and sends data to the Favoriot IoT Platform via Wi-Fi.

#include <WiFi.h>
#include <HTTPClient.h>
#include <Servo.h>

// Wi-Fi Information
const char* ssid = "Your_WiFi_SSID";
const char* password = "Your_WiFi_Password";

// Favoriot Information
const char* favoriot_host = "https://apiv2.favoriot.com/v2/streams";
const char* apiKey = "Your_API_Key";
const char* device_developer_id = "Your_Device_Developer_ID";

// TCS3200 Color Sensor Pins
const int s2 = 4;
const int s3 = 5;
const int sensorOut = 6;

// Servo Motor Pin
const int servoPin = 9;
Servo sorterServo;

// Color Variables
int redFrequency = 0;
int greenFrequency = 0;
int blueFrequency = 0;

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

  // Wi-Fi Connection
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nConnected to Wi-Fi");

  // Pin Configuration
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);
  pinMode(sensorOut, INPUT);

  // Initialize Servo
  sorterServo.attach(servoPin);
  sorterServo.write(0); // Initial position
}

void loop() {
  // Read Color
  redFrequency = getColorFrequency(LOW, LOW);
  greenFrequency = getColorFrequency(HIGH, HIGH);
  blueFrequency = getColorFrequency(LOW, HIGH);

  // Determine Color
  String color = detectColor(redFrequency, greenFrequency, blueFrequency);
  Serial.println("Detected Color: " + color);

  // Move Servo Based on Color
  moveServo(color);

  // Send Data to Favoriot
  sendDataToFavoriot(color);

  delay(5000); // Wait before reading the next object
}

int getColorFrequency(int s2State, int s3State) {
  digitalWrite(s2, s2State);
  digitalWrite(s3, s3State);
  return pulseIn(sensorOut, LOW);
}

String detectColor(int red, int green, int blue) {
  if (red < blue && red < green) {
    return "Red";
  } else if (blue < red && blue < green) {
    return "Blue";
  } else if (green < red && green < blue) {
    return "Green";
  } else {
    return "Unknown";
  }
}

void moveServo(String color) {
  if (color == "Red") {
    sorterServo.write(30); // Position for red objects
  } else if (color == "Blue") {
    sorterServo.write(90); // Position for blue objects
  } else if (color == "Green") {
    sorterServo.write(150); // Position for green objects
  } else {
    sorterServo.write(0); // Default position
  }
  delay(1000); // Wait for servo to move
  sorterServo.write(0); // Return to initial position
}

void sendDataToFavoriot(String color) {
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    http.begin(favoriot_host);
    http.addHeader("Content-Type", "application/json");
    http.addHeader("apikey", apiKey);

    String jsonData = "{\"device_developer_id\":\"" + String(device_developer_id) + "\",\"data\":{\"color\":\"" + color + "\"}}";

    int httpResponseCode = http.POST(jsonData);

    if (httpResponseCode > 0) {
      String response = http.getString();
      Serial.println("Data sent: " + response);
    } else {
      Serial.println("Failed to send data");
    }
    http.end();
  } else {
    Serial.println("Wi-Fi not connected");
  }
}


Note:

  • Replace Your_WiFi_SSID, Your_WiFi_Password, Your_API_Key, and Your_Device_Developer_ID with your actual information.
  • Ensure you have included the necessary libraries such as WiFi.h, HTTPClient.h, and Servo.h in your Arduino IDE environment.

Reference

Favoriot Documentation

Disclaimer

This article provides a step-by-step guide. The source code may need adjustments to fit the final project design.

Podcast also available on PocketCasts, SoundCloud, Spotify, Google Podcasts, Apple Podcasts, and RSS.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share This

Share this post with your friends!

Discover more from IoT World

Subscribe now to keep reading and get access to the full archive.

Continue reading