Click here to Skip to main content
15,868,016 members
Articles / Internet of Things / Arduino

A Tour of the Arduino Mega 2560+WiFi R3

Rate me:
Please Sign up or sign in to vote.
5.00/5 (12 votes)
30 Oct 2020CPOL11 min read 47.4K   8   21
Using this cheap, powerful little device to make magic
This article is about what to do with the device and its many clones after they arrive in the mail, sans instructions. It will take you through the process of setting up the development environment and configuring the device. The latter can get difficult, and even a little dangerous if you're unleashed without any instructions. Use this article to guide your efforts and save yourself some pain.

Arduino Mega 2560+WiFi

Introduction

The Arduino Mega 2560+WiFi is an Arduino board geared toward robotics, so it provides many inputs and outputs, both analog and digital, and not one, but four! hardware UARTs for doing serial communication. The extra hardware serial is nice to use for debugging because it can be logged to, even as you use the other hardware serial for talking to the onboard ESP8266 WiFi tranceiver. The dozens of inputs and outputs can be used to control devices or read from sensors, and they give you enough to run something like a robot.

Unfortunately. these tend to come with no software and no instructions - nothing, not even a datasheet. After a day or two of fiddling, and stepping on a proverbial landmine, I have got my feet under me and I aim to teach you how to use one of these things without bricking it. Yes, you can brick them - or at least brick their WiFi tranceiver. For those of you that don't know what "bricking" is, I suppose you're lucky. Bricking is when you turn a device into a brick, a paperweight, or a doorstop. You get the idea - garbage. That's the fun side of flashable hardware.

Here we go, and good luck!

Why this Particular Board?

Given the relative costs of these boards, I think the Arduino Mega 2560+Wifi and its clones are worth a little extra if for no other reason than having four hardware serial UARTs. While Arduinos can interact with a serial device by emulating a UART in software, they can't get baudrates of 115200 while doing so. With the onboard WiFi widget tying up one hardware serial port, it's nice to have the main one available as a COM port over the USB connection. This is only possible because there are at least two hardware serial ports built in. This way, we can use one for debug or otherwise information spew while the other is tied up talking over the WiFi tranceiver.

It should be noted that other, cheaper boards like the Arduino Uno can still use an ESP8266 WiFi tranciever but they cannot communicate with it at high speeds since they have no extra hardware serial to tap. 

The other nice thing is the arsenal of analog and digital I/O pins to use. You are not likely to run out of I/O for most projects. You can connect so many sensors and motors and other widgets to this thing, it boggles the mind, so if your project expands in scope over time, the board can grow with you.

Finally, this board appears to run quite happily off of USB (or AC, or 9v battery) power whereas configurations where the ESP8266 wifi gadget is external instead of integrated need an extra power supply for the ESP8266 in order to be reliable. I have had no problems powering the integrated setup off of USB alone.

In short, this board has it all and in a low power configuration with a small form factor.

Unwrapping and Installing this Mess

Unboxing the Mega 2560 + WiFi R3

I like to dive in to things in order to understand them, and it gets me into trouble. I like trouble, so we're going to start by unboxing the thing, messing with it, and turning it on. Don't worry, we won't brick it yet. I'll save that for later in the article. Kidding!

The first thing we need to do is verify that the 8x dip switch array is configured properly. I think all the boards here are pretty much the same, because they're all clones. It should be near the center of your board, and look something like this:

8x Dip Switch Array

Sorry about the fuzzy picture. My phone's camera wouldn't cooperate. The blue smudges are because of ink from the pen I had to use to toggle the switches. Unless you're doing something we aren't going to cover here, you'll want your switches set up to enable the WiFi and wire it into the 4th hardware serial bus, plus allow for flashing the Mega. To do this, you'll need to set the first four switches (1-4) to ON and the second four switches (5-8) to OFF. I had to figure this out by poring over the bits and pieces of information I could find online, plus a little bit of experimentation coupled with educated guesses. I'm pretty confident in it. Don't give me that look.

Also, there's a switch on your board with TXD0 on one side and TXD3 on the other. My camera is awful, but you'll find it near the headers for the lower digital I/O pins. Make sure the switch is set to the TXD3 side.

Once you have that sorted out, there should be one of those printer style USB cables or a microUSB cable that came with it, depending on your particular board. Mine has microUSB. Just plug it into your computer and into the board and enjoy the tiny light show.

Installing and Configuring the Arduino IDE

Next go here to download the IDE and install it. I use the Linux version, but many of you will probably use the Windows version. With the Linux version, you must fiddle with the permissions on your /dev/ttyXXX stuff to make it work. Luckily, they cover that here.

Once it's installed, you can launch it, but be careful about the "Tools" menu. I destroyed my ESP8266 firmware by messing around with it. I recovered but it took a night and some hair pulling. I'll walk you through safely configuring it to work with your device so you don't go through what I did.

You'll want to go under the Tools menu and ensure your Board is set to "Arduino Mega or Arduino Mega 2560" and your Processor is set to "ATmega2560". Do not ever change that unless you're certain you know what you're doing.

Installing the WiFi Library

The next thing you'll want to do is ignore everything you read online about programming against the ESP8266 WiFi device - maybe including this article. The wrinkle is there are several different libraries, and even different ESP8266 firmware which you can use. That's what got me into trouble. We won't be using custom ESP8266 firmware. We will be using the stock "AT firmware" for the ESP8266 device. We will be using a reliable library called WiFiEsp to communicate with the onboard ESP8266 using the default "AT command set" exposed by the ESP8266. This makes your WiFi hardware communicate using a similar command style as old Hayes style modems did. Different firmware can change this command set to something else like Micropython. It sounds cool. It might be. We aren't getting within a mile of that here because I don't want any of you coming after me for bricking part of your device.

Instead, we'll be using the WiFiEsp library which wraps the underlying AT commands with a nice API you can use to scan and connect to WiFi networks. I'm not going to cover the library beyond some basics, because it's just a bit outside the scope here. You'll want this library installed to take advantage of that brilliant little ESP8266 WiFi widget. I had problems installing the latest versions of this due to checksum failures, but it seems after I installed a slightly older version, it updated itself to the latest one. You can find it under Tools|Manage Libraries... You'll use the resulting dialog to search for WiFiEsp and install it. If it gives you errors, try previous versions like I did.

Coding this Mess

The Arduino IDE gives you a cute little C(ish?) compiler to use. If you're not familiar with C, it doesn't matter that much. Once you do away with pointer operations, there's not much that's intimidating about it and you won't need anything nasty in C to make it work. It's all pretty simple and the example code is pretty decent once you kind of know your way around. Let's get started.

The Basics

There are two methods every Arduino app will have, which are setup() and loop(). The first method is called once upon reset or power up. The second method is simply called repeatedly after that until reset or shutdown. They take no parameters and return no values. For those of you familiar with C, there is no main():

C++
void setup() {
  Serial.begin(115200); 
} 
void loop() {
  Serial.println("Hello world!");
  delay(1000);
}

The above hello world app transmits "Hello world!" over the USB's exposed COM/serial port at 115200 baud. After hitting the Upload button or Sketch|Upload, you can then use Tools|Serial Monitor to see the output of the above code.  Once every second, it will transmit another line of "Hello World!" over the serial port. Keep this technique in mind as it's your primary way to log or otherwise debug your code. What this means is if you plug this device in to a USB port, the owner of that port can see all of those happy status messages you'll find emitted in the examples with print() and println() by monitoring the virtual COM/serial port that's exposed for incoming data. 

That's cool, but now we should cover some of those pins and stuff we can use to wire up widgets and sensors. I'll only cover the digital ones here. The Arduino IDE comes with sample code for pretty much everything.

Each digital I/O pin jumper on your board should be labeled with a number. That number is the pin's ID in software. There appears to be 54 of the digital I/O pins on my board. Some are otherwise in use by the hardware serial ports. They can still be connected to but usually they should be left alone. This includes pin 0 and pin 1, so for your own projects, start at pin 2. The thing that's cool about these is they can be used for input or output. You can set the pinMode() in code to change it like:

C++
pinMode(2,INPUT); // pin 2 can now be read from.
pinMode(3,OUPUT); // pin 3 can now be written to.

I think some of these pins might be +3.3vdc so unless otherwise labeled as +5vdc, I never feed more than 3.3+vdc. Be careful. Don't fry your little board.

Now you can use digitalRead() and digitalWrite() to get or set the power value at the pin. 

C++
if(0!=digitalRead(2)) 
  Serial.println("pin 2 is high!");

digitalWrite(3,HIGH);

Coding against WifiEsp

Like the above, I aim to give you just enough to get you going plus there are many examples under File|Examples|WiFiEsp to get you started.

We'll be using serial communication to talk to our Wifi gadget. A lot of the code assumes you're using a software serial interface, but our fancy board has hardware serial. The code in the examples accounts for that sort of, but it expects that the WiFi gadget is connected on the 2nd hardware serial port, Serial1 whereas on our board, it will be Serial3. I'll show you below. First, the following is what you can expect to find at the beginning of any of the example code.

C++
#include "WiFiEsp.h"

// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX
#endif

That's no good for us. This is more like what you would do for an Uno board with an external ESP8266 wired into it or something. We're going to have to edit this code. Since we're coding specifically for our board, we don't need the #ifndef conditional, or the #include inside of it which will simplify the code. Finally, as I mentioned, our WiFi widget is attached to Serial3 on this board, not Serial1. Due to this, we can simply eliminate the lines from the above that follow #include "WifiEsp.h". We don't need them anymore. However, we must change any reference to Serial1 in the code that follows to Serial3. Usually that only means changing two lines, which is good for us:

C++
Serial1.begin(9600);

becomes:

C++
Serial3.begin(115200);

Changes are in bold. Notice we also changed the 9600 value to 115200. This is the baudrate and we've increased it because the lower value was required to accomodate software serial. We have fancypants hardware serial which is faster, so let's go faster.

The other line to change in the example code is:

C++
WiFi.init(&Serial1);

which becomes:

C++
WiFi.init(&Serial3);

You'll also see references to Serial.begin(), Serial.print() and similar in the example code. Serial is essentially "Serial0", and we already covered it somewhat before. It refers to our first hardware serial port, one that is exposed as a virtual COM/serial port over the USB connector. Just don't confuse Serial and Serial3 in the code since they are two separate serial ports.

The above modifications should serve you for most, if not all of the WiFiEsp examples under the file menu, which should get you going with the WiFi communication. Use the example code to work out the specifics. I may cover it in a future article. 

Points of Interest

The ESP8266's Tensilica CPU is perhaps the more interesting of the two onboard processors, and considerably more powerful but it's simply dedicated to handling the WiFi comm and TCP/IP stack. The ESP8266 WiFi adapter contains its own TCP/IP stack and CPU operating at 80mhz vs the Mega at 16mhz so it's even more powerful than the Mega. Apparently some packages can configure this setup to slave the Mega CPU to the Tensilica CPU in the ESP8266 which can lead to a very interesting and powerful little microdevice. This is fascinating to me, but it's also a huge pain and easy to mess up. I accidentally flashed its firmware by messing around in the Arduino IDE. We won't be doing that here, at least in this article, but it's food for thought.

History

  • 30th October, 2020 - Initial submission

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Just a shiny lil monster. Casts spells in C++. Mostly harmless.

Comments and Discussions

 
QuestionHow did you mange to flash the original ESP8266 firmware??? Pin
Member 154215577-Nov-21 2:06
Member 154215577-Nov-21 2:06 
AnswerRe: How did you mange to flash the original ESP8266 firmware??? Pin
honey the codewitch7-Nov-21 4:26
mvahoney the codewitch7-Nov-21 4:26 
Question8266 not responding to AT commands Pin
CharlieSimon28-Oct-21 6:54
CharlieSimon28-Oct-21 6:54 
AnswerRe: 8266 not responding to AT commands Pin
honey the codewitch28-Oct-21 6:57
mvahoney the codewitch28-Oct-21 6:57 
GeneralRe: 8266 not responding to AT commands Pin
CharlieSimon28-Oct-21 7:04
CharlieSimon28-Oct-21 7:04 
GeneralRe: 8266 not responding to AT commands Pin
honey the codewitch28-Oct-21 7:42
mvahoney the codewitch28-Oct-21 7:42 
GeneralRe: 8266 not responding to AT commands Pin
CharlieSimon28-Oct-21 10:36
CharlieSimon28-Oct-21 10:36 
QuestionIts not working Pin
Miroslava Vavrová19-Aug-21 8:58
Miroslava Vavrová19-Aug-21 8:58 
AnswerRe: Its not working Pin
honey the codewitch19-Aug-21 9:17
mvahoney the codewitch19-Aug-21 9:17 
AnswerRe: Its not working Pin
Member 157263403-Aug-22 3:35
Member 157263403-Aug-22 3:35 
PraiseProgress Pin
Member 1504554713-Jan-21 20:46
Member 1504554713-Jan-21 20:46 
QuestionArduino's More Capable Relatives Pin
BillKru3-Nov-20 19:48
BillKru3-Nov-20 19:48 
AnswerRe: Arduino's More Capable Relatives Pin
honey the codewitch4-Nov-20 4:28
mvahoney the codewitch4-Nov-20 4:28 
GeneralMy vote of 5 Pin
Greg Utas31-Oct-20 12:26
professionalGreg Utas31-Oct-20 12:26 
GeneralRe: My vote of 5 Pin
honey the codewitch31-Oct-20 15:46
mvahoney the codewitch31-Oct-20 15:46 
GeneralRe: My vote of 5 Pin
Member 104998532-Nov-20 6:17
Member 104998532-Nov-20 6:17 
GeneralRe: My vote of 5 Pin
honey the codewitch2-Nov-20 6:28
mvahoney the codewitch2-Nov-20 6:28 
GeneralRe: My vote of 5 Pin
Michael Streeter3-Aug-21 2:58
Michael Streeter3-Aug-21 2:58 
GeneralRe: My vote of 5 Pin
honey the codewitch3-Aug-21 3:44
mvahoney the codewitch3-Aug-21 3:44 
QuestionPictures Pin
Nelek31-Oct-20 12:15
protectorNelek31-Oct-20 12:15 
AnswerRe: Pictures Pin
honey the codewitch31-Oct-20 15:43
mvahoney the codewitch31-Oct-20 15:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.