કોડ શોધો

ESP32 ટ્યુટોરિયલ 38/55 - તમારા મોબાઇલ ફોનથી RGB LED ને નિયંત્રિત કરવું | SunFounder ની ESP32 IoT લર્નિંગ કિટ

ESP32 ટ્યુટોરિયલ 38/55 - તમારા મોબાઇલ ફોનથી RGB LED ને નિયંત્રિત કરવું | SunFounder ની ESP32 IoT લર્નિંગ કિટ

આ ટ્યુટોરિયલમાં, આપણે SunFounder ESP32 લર્નિંગ કિટમાંથી ESP32 મોડ્યુલનો ઉપયોગ કરીને RGB LED ને કેવી રીતે નિયંત્રિત કરવું તે શોધીશું. તમારા મોબાઇલ ઉપકરણમાંથી આદેશો મોકલીને, તમે LED નો રંગ બદલી શકો છો અથવા તેને સંપૂર્ણપણે બંધ કરી શકો છો. આ પ્રોજેક્ટ ESP32 ની ક્ષમતાઓનો ઉપયોગ કરે છે, જે સીમલેસ કનેક્ટિવિટી અને નિયંત્રણ માટે બિલ્ટ-ઇન Wi-Fi અને બ્લૂટૂથ સુવિધાઓનો લાભ લે છે.

ESP32_RGB_led_wires
ESP32_rgb_pin

RGB LED માં ત્રણ અલગ અલગ LED હોય છે: લાલ, લીલો અને વાદળી, જેને મિશ્રિત કરીને વિવિધ રંગો બનાવી શકાય છે. આ પ્રોજેક્ટમાં, તમે શીખી શકશો કે RGB LED ને યોગ્ય રીતે કેવી રીતે વાયર કરવું અને બ્લૂટૂથ આદેશોને પ્રતિસાદ આપવા માટે ESP32 ને કેવી રીતે પ્રોગ્રામ કરવું. ટ્યુટોરિયલ તમને આ કાર્યક્ષમતા પ્રાપ્ત કરવા માટે જરૂરી કોડ ઘટકો દ્વારા પણ માર્ગદર્શન આપશે (વિડિઓમાં 02:15 પર).

હાર્ડવેર સમજાવ્યું

આ પ્રોજેક્ટ માટેના પ્રાથમિક ઘટકોમાં ESP32 માઇક્રોકંટ્રોલર અને RGB LED નો સમાવેશ થાય છે. ESP32 એ બિલ્ટ-ઇન Wi-Fi અને બ્લૂટૂથ સાથેનું શક્તિશાળી મોડ્યુલ છે, જે તેને IoT એપ્લિકેશનો માટે આદર્શ બનાવે છે. આ પ્રોજેક્ટમાં, તે મોબાઇલ ઉપકરણમાંથી આદેશો પ્રાપ્ત કરવા અને તે મુજબ RGB LED ને નિયંત્રિત કરવા માટે સર્વર તરીકે કાર્ય કરશે.

RGB LED માં ચાર પિન હોય છે: એક સામાન્ય પિન (ક્યાં તો એનોડ અથવા કેથોડ) અને વ્યક્તિગત રંગો માટે ત્રણ પિન. સામાન્ય પિન ક્યાં તો પાવર સ્ત્રોત અથવા ગ્રાઉન્ડ સાથે જોડાય છે, જ્યારે અન્ય ત્રણ પિન કરંટ મર્યાદિત કરવા અને LED ને સુરક્ષિત કરવા માટે રેઝિસ્ટર દ્વારા ESP32 ના GPIO પિન સાથે જોડાય છે. આ સેટઅપ દરેક રંગની તેજસ્વિતાના ચોક્કસ નિયંત્રણને મંજૂરી આપે છે, જે રંગોની વિશાળ શ્રેણી બનાવે છે.

ડેટાશીટ વિગતો

ઉત્પાદક SunFounder
ભાગ નંબર RGB LED
સામાન્ય પિન પ્રકાર સામાન્ય એનોડ / સામાન્ય કેથોડ
ફોરવર્ડ વોલ્ટેજ (V) 2.0 - 3.2 V
મહત્તમ ફોરવર્ડ કરંટ (A) 20 mA
સામાન્ય કરંટ (A) 15 mA
રંગ રિઝોલ્યુશન 8 બિટ (0-255)
પેકેજ થ્રુ-હોલ / SMD

 

  • દરેક LED ચેનલ દ્વારા કરંટ મર્યાદિત કરવા માટે યોગ્ય રેઝિસ્ટર મૂલ્યો (સામાન્ય રીતે 220 ઓહ્મ) ની ખાતરી કરો.
  • વાયરિંગ પહેલાં સામાન્ય પિન રૂપરેખાંકન (એનોડ અથવા કેથોડ) તપાસો.
  • દરેક LED ને મોકલવામાં આવતા સિગ્નલને સમાયોજિત કરીને ડિમિંગ અને રંગ મિશ્રણ માટે PWM નો ઉપયોગ કરો.
  • શોર્ટ સર્કિટ ટાળવા માટે વાયરિંગમાં સાવચેત રહો; એક સમયે એક પિન જોડો.
  • યોગ્ય વાયરિંગની પુષ્ટિ કરવા માટે સેટઅપ પછી દરેક રંગને વ્યક્તિગત રીતે ચકાસો.

વાયરિંગ સૂચનાઓ

ES32-38_RGB_LED-wiring

RGB LED ને ESP32 સાથે વાયર કરવા માટે, બ્રેડબોર્ડ પર RGB LED મૂકીને શરૂ કરો. લાંબી પિન એ સામાન્ય પિન છે, જેને તમે ક્યાં તો હકારાત્મક વોલ્ટેજ (સામાન્ય એનોડ માટે) અથવા ગ્રાઉન્ડ (સામાન્ય કેથોડ માટે) સાથે જોડશો. જો તમે સામાન્ય એનોડનો ઉપયોગ કરી રહ્યા છો, તો લાંબી પિનને ESP32 પરના 3.3V પિન સાથે જોડો. સામાન્ય કેથોડ માટે, તેને GND પિન સાથે જોડો.

આગળ, ત્રણ 220 ઓહ્મ રેઝિસ્ટર લો અને દરેક રેઝિસ્ટરનો એક છેડો LED ના અનુરૂપ RGB પિન સાથે જોડો. રેઝિસ્ટરના બીજા છેડાને ESP32 GPIO પિન સાથે જોડો: LED ના લાલ પિનને GPIO 27 સાથે, લીલા પિનને GPIO 26 સાથે અને વાદળી પિનને GPIO 25 સાથે જોડો. છેલ્લે, ખાતરી કરો કે સામાન્ય પિન તમારા રૂપરેખાંકન (એનોડ અથવા કેથોડ) ના આધારે યોગ્ય રીતે જોડાયેલ છે.

કોડ ઉદાહરણો અને વોકથ્રુ

આ પ્રોજેક્ટ માટેનો કોડ RGB LED સાથે જોડાયેલા પિનને વ્યાખ્યાયિત કરીને શરૂ થાય છે. નીચેનો અંશ બતાવે છે કે પિન કેવી રીતે જાહેર કરવામાં આવે છે:

const int redPin = 27;
const int greenPin = 26;
const int bluePin = 25;

અહીં, redPin, greenPin, અને bluePin ને RGB LED ના દરેક રંગ ચેનલ માટે ESP32 પર ચોક્કસ GPIO નંબરો સોંપવામાં આવ્યા છે.

સેટઅપ ફંક્શનમાં, બ્લૂટૂથ શરૂ કરવામાં આવે છે, અને PWM સેટિંગ્સ લાગુ કરવામાં આવે છે. આ અંશ આ શરૂઆત દર્શાવે છે:

void setup() {
  Serial.begin(115200);      // સીરિયલ પોર્ટ શરૂ કરો
  setupBLE();                // બ્લૂટૂથ BLE શરૂ કરો

  ledcAttach(redPin, freq, resolution);
  ledcAttach(greenPin, freq, resolution);
  ledcAttach(bluePin, freq, resolution);
}

આ કોડ સીરિયલ કમ્યુનિકેશન શરૂ કરે છે અને બ્લૂટૂથ કાર્યક્ષમતા સેટ કરે છે જ્યારે નિયંત્રણ માટે RGB LED પિનને PWM ચેનલો સાથે જોડે છે.

છેલ્લે, લૂપ ફંક્શન પ્રાપ્ત બ્લૂટૂથ સંદેશાઓ તપાસે છે અને તે મુજબ LED રંગને સમાયોજિત કરે છે:

if (value == "red") {
  setColor(255, 0, 0); // લાલ
  Serial.println("red");
}

આ વિભાગમાં, જો પ્રાપ્ત મૂલ્ય "red" હોય, તો setColor ફંક્શનનો ઉપયોગ કરીને LED ને સંપૂર્ણ લાલ તેજસ્વિતામાં સેટ કરવામાં આવશે.

કોડની સંપૂર્ણ સમજ માટે, વિડિઓ ટ્યુટોરિયલ જોવાની ભલામણ કરવામાં આવે છે જ્યાં સંપૂર્ણ કોડ લેખની નીચે લોડ કરવામાં આવ્યો છે.

પ્રદર્શન / શું અપેક્ષા રાખવી

એકવાર બધું વાયર થઈ જાય અને કોડ અપલોડ થઈ જાય, તો તમે બ્લૂટૂથ દ્વારા તમારા મોબાઇલ ઉપકરણમાંથી RGB LED ને નિયંત્રિત કરી શકશો. "red", "green", "blue" વગેરે જેવા આદેશો મોકલીને, તમે LED ને તે મુજબ રંગ બદલતા જોશો. જો તમે "LED_off" મોકલો છો, તો RGB LED બંધ થઈ જશે. આદેશો યોગ્ય રીતે પ્રાપ્ત થઈ રહ્યા છે તેની પુષ્ટિ કરવા માટે કોઈપણ ડિબગિંગ સંદેશાઓ માટે સીરિયલ મોનિટર તપાસવાનું સુનિશ્ચિત કરો (વિડિઓમાં 10:45 પર).

વિડિઓ ટાઇમસ્ટેમ્પ્સ

  • 00:00 શરૂઆત
  • 1:59 RGB LED એટલે શું?
  • 6:01 RGB રંગ સમજાવ્યો
  • 10:01 દસ્તાવેજીકરણ પૃષ્ઠ
  • 11:19 વાયરિંગ સમજાવ્યું
  • 13:34 Arduino IDE માં ESP32 બોર્ડ અને COM પોર્ટ પસંદ કરવું
  • 15:15 Arduino કોડ
  • 18:02 તમારા ફોનનો ઉપયોગ કરીને RGB LED નિયંત્રિત કરવાનું પ્રદર્શન

છબીઓ

ESP32_rgb_pin
ESP32_rgb_pin
ESP32_RGB_led_wires
ESP32_RGB_led_wires
ES32-38_RGB_LED-wiring
ES32-38_RGB_LED-wiring
839-ESP32 Tutorial 38/55- Arduino code for controlling RGB LED using bluetooth app
ભાષા: C++
#include "BLEDevice.h"
#include "BLEServer.h"
#include "BLEUtils.h"
#include "BLE2902.h"

// Define RGB LED pins
const int redPin = 27;
const int greenPin = 26;
const int bluePin = 25;

// Define PWM frequency and resolution
const int freq = 5000;
const int resolution = 8;

// Define the Bluetooth device name
const char *bleName = "ESP32_Bluetooth";

// Define the received text and the time of the last message
String receivedText = "";
unsigned long lastMessageTime = 0;

// Define the UUIDs of the service and characteristics
#define SERVICE_UUID "8785d8b3-9d23-473b-aee5-3fabe2ba9583"
#define CHARACTERISTIC_UUID_RX "b2bcd13b-aab6-4660-92ae-40abf6941fce"
#define CHARACTERISTIC_UUID_TX "4219d86a-d701-4fd2-bd84-04db50f70fe2"

// Define the Bluetooth characteristic
BLECharacteristic *pCharacteristic;

void setup() {
  Serial.begin(115200);      // Initialize the serial port
  setupBLE();                // Initialize the Bluetooth BLE

  ledcAttach(redPin, freq, resolution);
  ledcAttach(greenPin, freq, resolution);
  ledcAttach(bluePin, freq, resolution);
}

void loop() {
  // When the received text is not empty and the time since the last message is over 1 second
  // Send a notification and print the received text
  if (receivedText.length() > 0 && millis() - lastMessageTime > 1000) {
    Serial.print("Received message: ");
    Serial.println(receivedText);
    pCharacteristic->setValue(receivedText.c_str());
    pCharacteristic->notify();
    receivedText = "";
  }

  // Read data from the serial port and send it to BLE characteristic
  if (Serial.available() > 0) {
    String str = Serial.readStringUntil('\n');
    const char *newValue = str.c_str();
    pCharacteristic->setValue(newValue);
    pCharacteristic->notify();
  }
}

// Define the BLE server callbacks
class MyServerCallbacks : public BLEServerCallbacks {
  // Print the connection message when a client is connected
  void onConnect(BLEServer *pServer) {
    Serial.println("Connected");
  }
  // Print the disconnection message when a client is disconnected
  void onDisconnect(BLEServer *pServer) {
    Serial.println("Disconnected");
  }
};

// Define the BLE characteristic callbacks
class MyCharacteristicCallbacks : public BLECharacteristicCallbacks {
  void onWrite(BLECharacteristic *pCharacteristic) {
    std::string value = std::string(pCharacteristic->getValue().c_str());
    if (value == "led_off") {
      setColor(0, 0, 0); // turn the RGB LED off
      Serial.println("RGB LED turned off");
    } else if (value == "red") {
      setColor(255, 0, 0); // Red
      Serial.println("red");
    }
    else if (value == "green") {
      setColor(0, 255, 0); // green
      Serial.println("green");
    }
    else if (value == "blue") {
      setColor(0, 0, 255); // blue
      Serial.println("blue");
    }
    else if (value == "yellow") {
      setColor(255, 150, 0); // yellow
      Serial.println("yellow");
    }
    else if (value == "purple") {
      setColor(80, 0, 80); // purple
      Serial.println("purple");
    }
  }
};

// Initialize the Bluetooth BLE
void setupBLE() {
  BLEDevice::init(bleName);                        // Initialize the BLE device
  BLEServer *pServer = BLEDevice::createServer();  // Create the BLE server
  // Print the error message if the BLE server creation fails
  if (pServer == nullptr) {
    Serial.println("Error creating BLE server");
    return;
  }
  pServer->setCallbacks(new MyServerCallbacks());  // Set the BLE server callbacks

  // Create the BLE service
  BLEService *pService = pServer->createService(SERVICE_UUID);
  // Print the error message if the BLE service creation fails
  if (pService == nullptr) {
    Serial.println("Error creating BLE service");
    return;
  }
  // Create the BLE characteristic for sending notifications
  pCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_TX, BLECharacteristic::PROPERTY_NOTIFY);
  pCharacteristic->addDescriptor(new BLE2902());  // Add the descriptor
  // Create the BLE characteristic for receiving data
  BLECharacteristic *pCharacteristicRX = pService->createCharacteristic(CHARACTERISTIC_UUID_RX, BLECharacteristic::PROPERTY_WRITE);
  pCharacteristicRX->setCallbacks(new MyCharacteristicCallbacks());  // Set the BLE characteristic callbacks
  pService->start();                                                 // Start the BLE service
  pServer->getAdvertising()->start();                                // Start advertising
  Serial.println("Waiting for a client connection...");              // Wait for a client connection
}

void setColor(int red, int green, int blue) {
  // For common-anode RGB LEDs, use 255 minus the color value
  ledcWrite(redPin, red);
  ledcWrite(greenPin, green);
  ledcWrite(bluePin, blue);
}

સંસાધનો અને સંદર્ભો

ફાઇલો📁

કોઈ ફાઇલો ઉપલબ્ધ નથી.