I need help with transferring a code from arduino uno to esp32

so i have a thesis about smart parkings and i made a project and while making it and testing since im used to arduino uno more i programmed on it and when im finally done with every feature all working flawlassly with the arduino to transfer it to work as good in esp 32 i faced many issues and im not good with it . So i need help if anyone can help please contact me
ps
i used arduino uno R3
i have ESP32 Doit Dev Kit V1
i have a mux,buzzer,mrfc522,servo,dht22,flameSensor,TCRT5000 and LCD 4x16

Gonna need a lot more information than "help please"

-post your code
-post your schematic showing how everything is wired together and powered
-post the problem(s) you are having

1 Like

okay
first the code :

#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <MFRC522.h>
#include <DHT.h>
#include <Wire.h>


#define fan 8
#define fire 7
#define check 4
#define SS_PIN 10
#define RST_PIN 9
#define buzzer 2
#define servoPin 6
#define MAX_ALLOWED_CARDS 10
const int S0 = 5;
const int S1 = 3;
const int S2 = 7;

Servo servo;
LiquidCrystal_I2C lcd(0x27, 16, 4);
MFRC522 rfid(SS_PIN, RST_PIN);


String allowedUIDs[MAX_ALLOWED_CARDS] = { "F3 17 01 FD", "03 94 F0 F4", "E3 B0 B9 F7" };  // Add more UID's as needed
int numAllowedCards = 3;

/*String UID2 = "03 94 F0 F4";  //card ID
String UID = "F3 17 01 FD";*/

bool slot1 = 0;  //parking slots variables
bool slot2 = 0;
bool slot3 = 0;
bool slot4 = 0;
bool mux = 0;
int Temp;
DHT dht(A0, DHT22);

byte checkMark[] = {
  B00000,
  B00001,
  B00011,
  B10110,
  B11100,
  B01000,
  B00000,
  B00000
};

void readSensorData() {
  // Loop over each sensor
  for (int i = 0; i < 4; i++) {
    // Select the current sensor using the select pins
    digitalWrite(S0, HIGH && (i & B0001));
    digitalWrite(S1, HIGH && (i & B0010));
    digitalWrite(S2, HIGH && (i & B0100));

    // Read the value from the current sensor
    int sensorValue = analogRead(mux);
    //Serial.print(i);
    Serial.println(sensorValue);

    // Check if the sensor value is 1000 or higher
    bool isHigh = sensorValue >= 800;

    // Assign the sensor value to the corresponding slot variable
    switch (i) {
      case 0:
        slot1 = isHigh;
        break;
      case 1:
        slot2 = isHigh;
        break;
      case 2:
        slot3 = isHigh;
        break;
      case 3:
        slot4 = isHigh;
        break;
    }
  }
  digitalWrite(S0, LOW);
  digitalWrite(S1, LOW);
  digitalWrite(S2, HIGH);
  Serial.print("fire Value :");
  int sensorValue = analogRead(A0);
  Serial.println(sensorValue);
  if (analogRead(A0) <= 100) {
    lcd.clear();
    lcd.print("Fire Decetect Leave building IMMEDIATELY");
    servo.write(150);
    digitalWrite(fan, HIGH);
    while (analogRead(A0) <= 100) {
      for (int i = 0; i < 5; i++) {  // Repeat 5 times
        tone(buzzer, 2000, 200);     // Play a 2000 Hz tone for 200 ms
        delay(200);                  // Wait for 200 ms
      }
    }
    lcd.clear();
    digitalWrite(fan, LOW);
  }
  digitalWrite(S0, HIGH);
  digitalWrite(S1, LOW);
  digitalWrite(S2, HIGH);
  int Temp = dht.readTemperature();
  Serial.println(Temp);
  lcd.setCursor(2, 2);
  lcd.print(Temp);
  lcd.setCursor(4, 2);
  lcd.print("C");

  digitalWrite(S0, LOW);
  digitalWrite(S1, HIGH);
  digitalWrite(S2, HIGH);
  if (digitalRead(A0) == LOW) {
    lcd.clear();
    lcd.print("Parking Slots:");
    lcd.setCursor(0, 1);
    lcd.print("Slot1:");
    lcd.setCursor(6, 1);
    lcd.print(slot1);
    lcd.setCursor(8, 1);
    lcd.print("Slot2:");
    lcd.setCursor(14, 1);
    lcd.print(slot2);
    lcd.setCursor(-4, 2);
    lcd.print("Slot3:");
    lcd.setCursor(2, 2);
    lcd.print(slot3);
    lcd.setCursor(4, 2);
    lcd.print("Slot4:");
    lcd.setCursor(10, 2);
    lcd.print(slot4);
    // Check if all slots are full
    if (slot1 == 1 && slot2 == 1 && slot3 == 1 && slot4 == 1) {
      lcd.setCursor(-4, 3);
      lcd.print("--PARKING FULL--");
    }
    delay(2000);
    lcd.clear();
  }
  
  /*if (analogRead(A8) >= 800) {  //IR exit sensor
    servo.write(150);
    delay(3000);
    servo.write(50);
    readSensorData();
  }*/
}


void setup() {
  servo.write(70);
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  servo.attach(servoPin);
  SPI.begin();
  rfid.PCD_Init();
  dht.begin();
  delay(500);
  pinMode(buzzer, OUTPUT);
  pinMode(check, INPUT_PULLUP);
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  lcd.createChar(0, checkMark);
  pinMode(fan, OUTPUT);
  digitalWrite(fan, LOW);
}
void loop() {
  servo.write(50);
  readSensorData();
  // Display welcome message and temperature
  lcd.setCursor(4, 0);  //idle loop
  lcd.print("Welcome!");
  lcd.setCursor(1, 1);
  lcd.print("Scan your card");
  lcd.setCursor(-4, 3);
  lcd.print("May 4th, 2024 || 07:10PM");  //time function to do later

  // RFID card reading
  if (!rfid.PICC_IsNewCardPresent())
    return;
  if (!rfid.PICC_ReadCardSerial())
    return;

  // Card read successful
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Scanning");
  String ID = "";

  for (byte i = 0; i < rfid.uid.size; i++) {
    lcd.print(".");
    ID.concat(String(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "));
    ID.concat(String(rfid.uid.uidByte[i], HEX));
    delay(300);
  }
  ID.toUpperCase();
  Serial.print("Scanned RFID tag: ");
  Serial.println(ID);

  // if Parking full, handle accordingly, do not open
  while (slot1 == 1 && slot2 == 1 && slot3 == 1 && slot4 == 1) {
    for (int i = 0; i < 3; i++) {  // Repeat 3 times
      tone(buzzer, 800, 100);      // Play an 800 Hz tone for 100 ms
      delay(150);                  // Wait for 150 ms
    }
    tone(buzzer, 800, 800);  // Play an 800 Hz tone for 800 ms

    lcd.setCursor(2, 0);
    lcd.print("We Are Sorry");
    lcd.setCursor(2, 1);
    lcd.print("Parking Full");


    lcd.setCursor(-4, 2);
    lcd.print("Slot1:");
    lcd.setCursor(2, 2);
    lcd.print(slot1);
    lcd.setCursor(4, 2);
    lcd.print("Slot2:");
    lcd.setCursor(10, 2);
    lcd.print(slot2);

    lcd.setCursor(-4, 3);
    lcd.print("Slot3:");
    lcd.setCursor(2, 3);
    lcd.print(slot3);
    lcd.setCursor(4, 3);
    lcd.print("Slot4:");
    lcd.setCursor(10, 3);
    lcd.print(slot4);
    delay(1500);
    lcd.clear();
    return;
  }

  bool isValidUID = false;
  for (int i = 0; i < sizeof(allowedUIDs) / sizeof(allowedUIDs[0]); i++) {
    if (ID.substring(1) == allowedUIDs[i]) {
      isValidUID = true;
      break;
    }
  }

  // Card authentication
  if (isValidUID) {
    tone(buzzer, 1000, 200);
    delay(200);
    tone(buzzer, 1500, 200);
    servo.write(150);
    lcd.clear();  //unlocked screen
    lcd.setCursor(0, 0);
    lcd.print(" ---Unlocked---");
    lcd.setCursor(-6, 1);
    lcd.print("User Name");
    lcd.setCursor(-4, 2);
    lcd.print("Car Model");
    lcd.setCursor(-4, 3);
    lcd.print("Time left");
    delay(1500);
    servo.write(50);
    lcd.clear();
  } else {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Wrong card!");
    tone(buzzer, 1500, 200);
    delay(200);
    tone(buzzer, 1000, 200);
    delay(3000);
    lcd.clear();
  }
  digitalWrite(S0, LOW);
  digitalWrite(S1, HIGH);
  digitalWrite(S2, HIGH);
  if (digitalRead(A0) == LOW) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Adding New Card");
    // Check if there's space in the array to add a new card
    if (numAllowedCards < MAX_ALLOWED_CARDS) {
      allowedUIDs[numAllowedCards] = ID.substring(1);
      numAllowedCards++;
      Serial.print("Added new allowed ID: ");
      Serial.println(ID.substring(1));
    } else {
      Serial.println("Allowed IDs array is full.");
    }
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("New card added");
    lcd.setCursor(15, 0);
    lcd.write(byte(0));

    lcd.setCursor(0, 1);
    lcd.println("Card ID:         ");
    lcd.setCursor(-4, 3);
    lcd.print(ID);
    delay(2000);  // Debounce delay
    lcd.clear();
  }
}

second
the problems are endless you name it, nothing works in this piece of shit. if it wasnt for the smaller size and wifi support i wouldnt use it
but for example the servo motor doesnt work i tried another esp 32 library it didnt work as it should
i dont know how to connect the rfid scanner
there is problem with delays or something im not sure but when a fire is detected a buzzer should tone 5 times in the arduino its fine here its just a continues tone
thats how far i got till i decided i need help since nothing just works everything needs 5hours of searching to get it work with 9999 additional line of code and libraries and unnecessary stuff im very frustrated

Sounds like you need to start from scratch and build it up piece by piece .... ICD, Servo, Sensors, WiFi, etc. Only add one thing at a time and don't move on to the next until all previous ones are working together.

thats what i planned to do i got the lcd to work then i got the multiplexer to work and the temprature sensor then the flame sensor its just an analog read if its lower then a threshhold then there is fire and the code does the rest but then it doesnt work and the logic is correct sometimes it goes inside the fire loop sometimes it doesnt even tho the sensor shows with an led that its detecting a fire sometimes the whole esp just freezess and stops and i need to reset it . i looked for a servo library i found one it works on a new sketch that has the servo only on it but when i use it in my code it works at the start then just stops when a fire is detected im using fire only to test cuz thats how far i got i didnt go as far as plugging the rfid scanner im sure if i did more problems will appear

So how is everything wired up? You are moving from a 5V device (Uno) to a 3.3V device (ESP32) so you need to take care. The ESP32 won't tolerate 5V on its input pins if your sensors are still powered at 5V.

i measured the Vin and its outputting about 4.8v so im using it as the power (im powering my esp with usb)

okay. so that is how you are powering your ESP32. How about all your sensors? You can't just tie everything to V_in at 4.8V since the ESP32 i/o pins only tolerate 3.3V.

POST A SCHEMATIC, please.

oh that could be the problem ? cause im wiring everything to to v in and using it as my power source just like i used it with the arduino (vin=5V)
and schematics i dont have a simulation that i used i was just adding sensors and changing pins trying and fixing i have a paper that i made for the arduino when i went to the esp i just changed pins with respect to read only pins and analog ect

Most definitely. You need to check the datasheet of your devices to see if they can run at 3.3V or you need to install level shifters between the devices and your ESP i/o pins. It is not a trivial matter mixing 3.3V and 5V devices.

You could also switch to the new Uno R4 with Wifi if you are just trying to get wifi into your project. It is 5V, but also a new-ish board (and not an AVR chip) so some libraries do not support it yet.

i have a arduino wifi module do you reckon i should just stick to uno r3 and use wifi chip?
if so do you know how to wire it ? i think it has 8 pins

Which wifi module? I'm sure it has been done before if you google it

i have this im not sure of its name nor even sure it works with my arduino uno if you have any idea artical or video that could help that would be awesome
image

where did you purchase it? Just a picture isn't helpful.

It looks like a ESP-01

You could also switch to the new Uno R4 with Wifi

The Arduino Uno WiFi Rev 2 is also a board with wifi and 5 volt i/o.

It uses the ATmega 4809 processor so there may be issues with all the libraries you use being ported to that processor.

You can try test compiling for a proposed target with out the actual board for download to see what is supported.

Check all library compatibilities. I recently found that a display library works good on Uno, but nothing but errors on an ESP-12. Eventually found in the library documentation, the library was tested across many AVR products, but no Espressif products.

any idea how to wire it and program it and use it ?
and can it be used just like the esp32 integrated wifi?

Search the forum
For example:

PLEASE I need help asap for a project and im stuck !
so basically i have an arduino uno and i have this wifi module im trying to find way to connect it and use it to connect to a wifi so i can make a local website to show some informations i know it can be done better with an esp32 i have one i tried but its too hard