0% found this document useful (0 votes)
25 views8 pages

ESP32 Arduino Data Transmission Guide

Uploaded by

Tanim Ahmed
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views8 pages

ESP32 Arduino Data Transmission Guide

Uploaded by

Tanim Ahmed
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ESP32 and Arduino Uno Project: Data Transmission Over Wi-Fi

This document covers the details of the ESP32 and Arduino Uno project, where the Arduino Uno

collects sensor data

and sends it to the ESP32 over serial communication, and the ESP32 transmits this data over Wi-Fi

to a laptop or web

server. The project involves using the DHT11 sensor, ultrasonic sensor, and IR sensors to collect

environmental and

distance data. The ESP32 communicates with the Arduino Uno over Serial, processes the data, and

sends it via Wi-Fi.

The project aims to demonstrate a real-time data transmission system from an Arduino-based

sensor network to a

web interface via ESP32.

Hardware Connections

1. **Arduino Uno to ESP32 Wiring**:

- **TX (Arduino Uno Pin 1)** -> **RX (ESP32 Pin 16)**

- **RX (Arduino Uno Pin 0)** -> **TX (ESP32 Pin 17)**

- **GND (Arduino Uno Pin GND)** -> **GND (ESP32 Pin GND)**

2. **Arduino Uno to Sensors**:

- **DHT11**: Data Pin to **Pin 2** on Arduino

- **Ultrasonic**: Trigger Pin to **Pin 3**, Echo Pin to **Pin 4** on Arduino
- **IR Sensors**: Connected to **Analog Pins A0 to A7** on Arduino

3. **ESP32 to Wi-Fi**: Connect to your Wi-Fi network via the **3.3V** pin and **GND** for power.

Serial communication happens over the **TX/RX pins** (GPIO 16/17).

Arduino Uno Code

#include <DHT.h>

#include <NewPing.h>

// Pin Definitions

const int irSensor[] = {A0, A1, A2, A3, A4, A5, A6, A7}; // 8 IR sensors

const int motorPins[] = {3, 4, 5, 6, 7, 8, 9}; // Motor control pins (PWMA, AIN2, AIN1, STBY, BIN1,

BIN2, PWMB)

// DHT11 Sensor

#define DHTPIN 2 // DHT11 data pin

#define DHTTYPE DHT11 // Define sensor type

DHT dht(DHTPIN, DHTTYPE); // Initialize DHT11 sensor

// Ultrasonic Sensor (HC-SR04)

#define TRIG_PIN 3

#define ECHO_PIN 4

#define MAX_DISTANCE 200 // Maximum distance (in cm)

NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); // Initialize the ultrasonic sensor


void setup() {

// Initialize motor control pins

for (int i = 0; i < 7; i++) {

pinMode(motorPins[i], OUTPUT);

// Initialize IR sensors

for (int i = 0; i < 8; i++) {

pinMode(irSensor[i], INPUT);

// Start the DHT11 sensor

[Link]();

// Start the Serial Monitor for communication with ESP32

[Link](115200); // Set baud rate for communication with ESP32

// Start the motors

digitalWrite(motorPins[3], HIGH); // Enable motor driver (STBY)

void loop() {

int sensorValue[8]; // Array to store sensor readings

// Read the sensor values using direct ADC access (port manipulation)

for (int i = 0; i < 8; i++) {


sensorValue[i] = readADC(irSensor[i]); // Use the readADC function

// Read temperature and humidity from DHT11

float temperature = [Link](); // Celsius

float humidity = [Link](); // Percentage

// Read distance from Ultrasonic sensor

unsigned int distance = sonar.ping_cm();

// Send sensor readings to ESP32 via Serial

[Link]("Temp: ");

[Link](temperature);

[Link](" °CHumidity: ");

[Link](humidity);

[Link](" %Distance: ");

[Link](distance);

[Link](" cm");

// Send IR sensor values

for (int i = 0; i < 8; i++) {

[Link](sensorValue[i]);

[Link]("");

[Link](); // Send the data line by line


delay(100); // Small delay for stability

uint16_t readADC(uint8_t pin) {

uint8_t channel;

// Manually map the analog pin to its ADC channel

switch (pin) {

case A0: channel = 0; break;

case A1: channel = 1; break;

case A2: channel = 2; break;

case A3: channel = 3; break;

case A4: channel = 4; break;

case A5: channel = 5; break;

case A6: channel = 6; break;

case A7: channel = 7; break;

default: return 0; // Invalid pin

// Set the ADC channel and reference voltage (Vcc)

ADMUX = (1 << REFS0) | channel; // Use Vcc as reference voltage and select the channel

// Start the ADC conversion

ADCSRA |= (1 << ADSC); // Start conversion

// Wait for the conversion to finish

while (ADCSRA & (1 << ADSC)); // Wait while ADSC is still set (conversion in progress)
return ADC; // Return the ADC result

ESP32 Code

#include <WiFi.h>

#include <ESP32WebServer.h>

// Wi-Fi Credentials

const char* ssid = "your_wifi_ssid"; // Replace with your Wi-Fi SSID

const char* password = "your_wifi_pass"; // Replace with your Wi-Fi password

// Create an HTTP server on ESP32

ESP32WebServer server(80);

// Variables to hold data from Arduino

String sensorData;

// Initialize Serial communication

void setup() {

// Start Serial communication

[Link](115200); // Make sure this baud rate matches with Arduino Uno's baud rate

// Connect to Wi-Fi

[Link](ssid, password);
[Link]("Connecting to Wi-Fi...");

// Wait until the ESP32 connects to Wi-Fi

while ([Link]() != WL_CONNECTED) {

delay(1000);

[Link](".");

[Link]("Connected to Wi-Fi!");

// Set up HTTP server route

[Link]("/", HTTP_GET, []() {

[Link](200, "text/html", "<h1>Data Received from Arduino</h1><p>" + sensorData +

"</p>");

});

// Start the server

[Link]();

void loop() {

// Check if data is available from Arduino

if ([Link]()) {

sensorData = [Link]('

'); // Read the data sent from Arduino

// Print the received data to the Serial Monitor


[Link](sensorData);

// Handle HTTP requests

[Link]();

ESP32 and Arduino Uno Pinout Diagrams

Pinout diagrams of ESP32 and Arduino Uno:

**Arduino Uno to ESP32 Wiring**:

- **TX (Arduino Uno Pin 1)** -> **RX (ESP32 Pin 16)**

- **RX (Arduino Uno Pin 0)** -> **TX (ESP32 Pin 17)**

- **GND (Arduino Uno Pin GND)** -> **GND (ESP32 Pin GND)**

For further details, refer to the official pinout documentation for **ESP32** and **Arduino Uno**.

You might also like