Mega 2560 ESP32 connection

I'm new to programming with Arduino and the ESP32... :face_in_clouds:
And I would be very grateful if somebody could help me with connecting my ESP32 to a Mega 2560.
My ESP32 is already connected via Bluetooth to a PS4 Controller, I want to relay the data from the PS4 Controller trough the ESP32 to the Mega 2560.

I'm using the ESP32 NodeMCU with the following pinout:

ESP-32_NodeMCU_Developmentboard_Pinout.pdf (shopify.com)

The pinout of the Arduino Mega 2560:

Pinout-Mega2560rev3_latest.png (2571Γ—2572) (arduino.cc)

To begin I struggled trough many Youtube videos like this one:

ESP32 UART TUTORIAL AND BASIC DEMO - How to connect ESP32 to Arduino Uno Step by Step - YouTube

I followed this Video and fortunately it worked pretty fine :smile:

The code for the ESP32:

void setup() {
  Serial.begin(115200);
  Serial2.begin(9600, SERIAL_8N1, 16, 17);
}

void loop() {
  Serial.print("Data: ");
  Serial.println(Serial2.readString());
  delay(200);
}

The code for the Mega 2560:

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println("100");
  delay(1500);
}

  1. But how can I send data from the ESP32 to the Arduino Mega 2560, and how can I include the PS4 Controller into it?

I want to relay the data from the PS4 Controller trough the ESP32 to the Mega 2560

  1. Why is in the code from the tutorial a delay?
    Is the delay necessary for the performance?

  2. Which UART did I have to use and how would look the wire connection?

:thinking:

My advice would be: don't.

Why do you think you need to connect esp32 to Mega? For beginners, using more than one controller is usually a mistake. Even experts would only do such a thing after very careful thought and research to eliminate other ways to achieve their goal.

Ok, you need to connect to the PS4 via Bluetooth using the esp32. What do you need the Mega for? Do you see the need for lots of pins? There may be ways to do what you want with fewer pins, or add more pins to the esp32, so that the Mega is not needed.

1 Like

Because...why? Is it feasible to have the entire project run on just the ESP32? You do realize that an ESP32 is in itself a very capable, fully-functional microcontroller, right?

What do you mean by 'including the PS4 in it'? Is your question really about interfacing the ESP32 with the PS4, or interfacing the ESP32 with the Mega? If you can do the latter, it doesn't matter where the data comes from, whether it's from your PS4 or just random numbers; the Mega doesn't care.

It's probably just there to prevent digital diarrhea on the Serial monitor that goes so fast it cannot be read by human eyes.

Depends on what you want. Usually Serial0 is used for connecting to the Serial monitor on your computer so if you have more hardware UARTS on the Mega then go ahead and use one. The pinout will tell you which pins to connect to.
In other words: what have you tried/found so far and where do you get stuck?

At the end I want to control up to six stepper motors with the Mega 2560...
Because of that I thought its easier to control these stepper motors with the Mega2560 as with the ESP32...
I do this with two other guys for a project at my school... :neutral_face:

Ok, I can imagine in that scenario it's worth the hassle of getting both board to talk over Serial so you can have direct access to more GPIO's on the Mega board.

Because...why? Is it feasible to have the entire project run on just the ESP32? You do realize that an ESP32 is in itself a very capable, fully-functional microcontroller, right?

I fully agree with you but

At the end I want to control up to six stepper motors with the Mega 2560...
Because of that I thought its easier to control these stepper motors with the Mega2560 as with the ESP32...
I do this with two other guys for a project at my school... :neutral_face:

What are the stepper motors for? Why choose stepper motors over the other types of motor that are available?

It probably is easier with the Mega because it has so many pins. But it is much harder to use 2 controllers in one project, as you are just beginning to discover. Better to add more pins to the esp32. There are several ways to do this. Tell us more about the project and we can give better advice

Okay...

We want to create and build our own articulated robot and I have about one and a half year time, to do that.
In the last few days we struggled trough many different kinds of problems...
I am responsible in this project for the electronics and the robots programmation...
The other for the construction of this robot.
We already encountered an association where we may use their 3d printing machine...

We are just at the beginning of this project...
And Im really sorry for my english language skills... :face_in_clouds:

I hope my explanation was detailed enough for you... :sweat_smile:

Not really. You answered one of my questions with a vague answer (there are many kinds of robots). The other question you did not answer at all.

So basically...the entire project :wink:
(Edit: seriously though - be aware that you've been tasked with the most complex part of the project by far; I'm not too sure if I would be very happy with this task division in your place.)

18 months sounds like a realistic timeline to create something that moves, in a way. You'll encounter a lot of hurdles on the way and there'll be many choices to make. Such as the one you're facing now; which of the two evils (getting 2 boards to talk vs. getting stepper motors to work with very few GPIO pins) will you spend your time on?

You could investigate both routes for a bit and see what makes you feel the most confident that you'll pull it off.
Very often if also really helps if you can actually try stuff out in a hands-on manner. I usually end up buying many more types of components than I use in a particular project just because I need to investigate different options. I don't know if your budget allows this, however.

Perhaps a little more research into the ESP32 capabilities would be beneficial? API Reference - ESP32 - β€” ESP-IDF Programming Guide latest documentation (espressif.com). An ESP32 S2 with a few extra pins is more then capable of doing WiFi, running the stepper motors and the other things.

The ESP32 has an internal module called the MCPWM for motor control, Motor Control Pulse Width Modulator (MCPWM) - ESP32 - β€” ESP-IDF Programming Guide latest documentation (espressif.com). While you'll still need an external motor driver, the ESP32 can handle the motors. Oh the MCPWM, once programmed does the thing, without CPU intervention.

Which is great, but won't run 6 motors. So you'd still have to augment this with for instance GPIO expanders and then find/modify a stepper library to drive motors over the expander.

O Im sorry...

image

I have to say, that we are just at the beginning of this project, we want a robot with six axis... because of that we want to control six stepper motors...
The picture above is just an example for you what kind of robot I meant...
We wanted to use stepper motors, because a friend of mine who is quit good in dealing with technique said that stepper motors are more powerful than, for example "normal" servos

I'm sorry to have to tell you that your friend does not understand the advantages and disadvantages of different types of motor. Stepper motors are not, by their nature, a powerful type of motor. For a robot arm, servo motors are probably the more appropriate choice.

Stepper motors have the advantage of very high precision, repeatable movement. That's why they are used for C&C routers, 3D printers and laser cutters. They are, for their size, not "powerful" if you mean high speed or torque. They are also inefficient in terms of power usage and complex to drive, compared to other types of motor. They also have no built-in sensor to give them knowledge of their current position.

Servo motors, for the same size of motor, are more efficient and have higher torque because they contain a gearbox. They also have a built-in sensor so that they know their position at all times, making them more appropriate for a robot arm, in my opinion. You would not need extra pins on the esp32 to control 6 servo motors, I think.

1 Like

And if you do want to use 2 microcontrollers, I used 3 in my Hexapod, by using ESP32's, they can communicate wirelessly. With my hexapod, 1 ESP acts as the brain, one ESP32 is for LIDAR imaging and one ESP32 just runs the servos. If I was to do the project over, I'd just use 2 ESP32's.

The Brain is a ESP32 WROOM. I use the extra ram on the WROOM to store a map of the general area made with the LIDAR's ESP.

1 Like

Haha, yes :joy: :joy:
( And now it dawns on me..., but quitting is no solution... :laughing: )

I never heard of the second of your evils :no_mouth:
To this time I prefer the problem of

getting 2 boards to talk

But I will also investigate the second evil you said....
Have you an link with an tutorial for your second evil?

getting stepper motors to work with very few GPIO pins

And yes it seems like I have to buy some more things :laughing:

I will discuss this problem with my project members...
I shared the link of this discussion with them...
Thank you really much! :star_struck:

You would not need extra pins on the esp32 to control 6 servo motors, I think.

Did I understood you correctly?
For the whole robot arm are only one ESP32 enough? :scream:

For the robot arm I built using servos, one ESP32 was enough.

I will order at least one more ESP32 to try your approach :grin:
What is

LIDAR imaging

:thinking:

Something like this: Use an ESP32 and ToF Sensor to Build a Cheap LIDAR Platform - Hackster.io.

And if I was to do that project over again, I'd use machine learning and a camera for imaging. I got one project where I am using a thermal camera to detect and discriminate between humans and animals with machine learning. I'm using KNN for that project. I also am using TensorFlow for image processing but the TensorFlow things are done by a Raspberry Pi.