Need help connecting my Mega 2560 board with Wemos D1 mini

Hello,
can somebody please confirm is it possible to connect my Arduino Mega 2560 board with Wemos Esp8266 D1 mini (I want to read the values of my sensors on Blynk)? if so how can that be achieved?
The Wi-Fi board itself is too small for my project as I need 6 HC-SR04 sensors, 6 pumps, 2 servos, DC motor and LCD.

Thanks in advance.

Does a serial connection is a suitable solution for you?

1 Like

Anything is suitable for me as long as I can connect the two and monitor the values from the cloud.
Can you help me out a little abot that serial connection you just mentioned, what do I need to do?

Generally speaking, a serial connection between two Arduino/WeMos can be made by connecting two serial (e.g. SoftwareSerial) pins with a crossed configuration (TX pin of 1 to RX pin of 2 and viceversa), and as soon as this channel is ready you can send bytes/strings from one and receive from the other, implementing some sort of protocol.
But I'm sorry to say I missed the fact that you want to read values via Blynk, I don't really know how you can "pass" such communication from serial to Blynk as I never used Blynk.
I hope someone else will be able to help you better than me...

It's a little clearer now, thank you for your time.
Yeah I hope so too haha

if you have AT firmware in esp8266, you can use the BlynkESP8266_Lib.
Blynk has examples

How can I check if I have AT firmware on my device? I know how to do it on General ESP8266 device but I have no idea on how to do it on WeMos D1 mini...

the same way

I just tried but the AT commands are not working

I've successfully made them communicate with serial RS485.

  1. Code for Arduino Mega
#include "Arduino.h"
 
#include <SoftwareSerial.h>
#define RXPin        10  // Serial Receive pin
#define TXPin        11  // Serial Transmit pin
 
//RS485 control
#define SERIAL_COMMUNICATION_CONTROL_PIN 5 // Transmission set pin
#define RS485_TX_PIN_VALUE HIGH
#define RS485_RX_PIN_VALUE LOW
 
SoftwareSerial RS485Serial(RXPin, TXPin); // RX, TX
 
int byteSend;
 
void setup()  {
  Serial.begin(9600);
 
  pinMode(SERIAL_COMMUNICATION_CONTROL_PIN, OUTPUT);
  digitalWrite(SERIAL_COMMUNICATION_CONTROL_PIN, RS485_RX_PIN_VALUE);
  RS485Serial.begin(9600);   // set the data rate
 
  delay(500);
 
  digitalWrite(SERIAL_COMMUNICATION_CONTROL_PIN, RS485_TX_PIN_VALUE); // Now trasmit
  Serial.println("Send data!");
  RS485Serial.print("Hello world!"); // Send message
}
 
void loop() {
      digitalWrite(SERIAL_COMMUNICATION_CONTROL_PIN, RS485_RX_PIN_VALUE);  // Disable RS485 Transmit
 
        if (RS485Serial.available()){
            Serial.println("Response available!");
            Serial.println(RS485Serial.readString());
        }
 
      delay(100);
}
  1. Code for WeMos D1 mini
#include "Arduino.h"
 
#include <SoftwareSerial.h>
#define RXPin        D2  // Serial Receive pin
#define TXPin        D3  // Serial Transmit pin
 
//RS485 control
#define SERIAL_COMMUNICATION_CONTROL_PIN D0 // Transmission set pin
#define RS485_TX_PIN_VALUE HIGH
#define RS485_RX_PIN_VALUE LOW
 
SoftwareSerial RS485Serial(RXPin, TXPin); // RX, TX
 
int byteSend;
 
void setup()  {
  Serial.begin(9600);
 
  pinMode(SERIAL_COMMUNICATION_CONTROL_PIN, OUTPUT);
  digitalWrite(SERIAL_COMMUNICATION_CONTROL_PIN, RS485_RX_PIN_VALUE);
  RS485Serial.begin(9600);   // set the data rate
}
String dataReceived;
bool isDataReceived = false;
void loop() {
    digitalWrite(SERIAL_COMMUNICATION_CONTROL_PIN, RS485_RX_PIN_VALUE);// Init receive
 
    if (RS485Serial.available()){
        dataReceived = RS485Serial.readString();
        Serial.print("Data received ");
        Serial.println(dataReceived);
        isDataReceived = true;
        delay(10);
    }
 
    if (isDataReceived){
        delay(111);
        digitalWrite(SERIAL_COMMUNICATION_CONTROL_PIN, RS485_TX_PIN_VALUE); // Init transmit
        Serial.println("Send response!");
        RS485Serial.print("OK --> ");
        RS485Serial.println(dataReceived);
        isDataReceived = false;
    }
 
    delay(111);
}

Does this help me somehow?

not really

1 Like

I would suggest expanding the I/o of the Wemos rather than attempting to connect it to a Mega.

Don't forget that the Mega is a 5V device and the Wemos is 3.3V so logic level shifting would be required if you go ahead with that.

You don't say what kind of LCD, so I'll assume that's a 16x2 or 20x4 character LCD. You can attach an i²c backpack to these so that only 2 pins are needed to connect it to the Wemos.

You can use a pcf8575 I/o expander to provide an extra 16 pins for your pumps, motor and other devices

The ultrasonic sensors are a little more difficult. You cannot use pins of the pcf chip for these sensors because accurate timing of the echo signal is required. The trigger and echo pins of these sensors can be connected with a resistor so that only one wemos/Mega pin is required for both functions. I would experiment with a 74hc4051 multiplexer to allow a single wemos pin to trigger and measure the echo signal of whichever of 6 of your sensors is selected by the multiplexer. The multiplexer channel selection can be done with 3 pins from the I/o expander.

With this arrangement, only 3 wemos pins used so far, so you can use 2 more wemos pins for your servos.

Wow thank you very much for this detailed explanation, this is a great alternative.
You mentioned connecting the trigger and echo pins with a resistor so that only one pin is required for it to work, how can I do that exactly? and would the code for it change in some way if I do it like that?

Here's an example of how to connect them.
main-qimg-c93cec5fd2c8883f1b9c9a4484fc034b
The pin labelled "ICP1" would need to be a wemos pin. The pins labelled 2, 3, 4 can be pins on the pcf expander.

Most of these HC-SR04 sensors run only at 5V (only special ones can run at 3.3V), so you should also use a pair of resistors (eg. 10K & 22K) to reduce the 5V echo signal to 3.3V.

If you are using a library with the sensors, I don't know if or how you can use 1 pin per sensor, but if you are simply using a few lines of code like digitalWrite(), delayMicroseconds() and pulseIn(), then it's just a matter of adding a couple of pinMode() lines to change the pin to OUTPUT for sending the trigger pulse, then back to INPUT for receiving the echo signal.

Thank you very much man.
Just one more follow up, I must have the 74hc4051 multiplexer for this right?

There may be other chips which could perform the same function, for example 74hc4067 which is the 16 channel version of 74hc4051. In order to use only one wemos pin, the multiplexer must be bi-directional, because the Wemos pin will be switching between INPUT and OUTPUT modes. Not all multiplexers are bi-directional.

If you choose or need to to use 2 pins for the sensors, you could use other uni-directional multiplexers/demultiplexers, such as 74hc138 or 74hc151 but 74hc4051 is more commonly used and easier to use. So even if you continue using 2 pins for the sensors, using 2x 74hc4051 is probably the simplest solution.

1 Like

Great information, thank you!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.