interfacing arduino uno wtih nodemcu

NodeMCU is great for connecting cloud and arduino is great at talking with different sensors. nodemcu has only one analog pin. In this blog we will see how to connect arduino to nodemcu and post data to a mqtt broker. Arduino will take temperature readings and send the readings to nodemcu over serial connection. Nodemcu will send a mqtt message for every reading it receives. If you have just one sensor to monitor you can directly use the analog input available on nodemcu, see this blog on how to use the analog pin of nodemcu.

On Arduino side, we will take sample periodically and send a JSON message over softserial to nodemcu. Please note that Arduino uno works on 5v and nodemcu works 3v3 level. So you should use a level shifter to connect arduino soft serial pins to the nodemcu uart port.

On nodemcu side we need to receive the messages sent by arduino over serial. Arduino will send the messages in JSON format. We will not be doing any processing on nodemcu (It is possible to process the messages before sending to cloud, for example you can compare the present reading with the previous published messages and send the message only if the reading is different) . You need to have working MQTT broker account. You can follow this blog to create and test mqtt account.

Arduino Code :

Nothing fancy on arduino side, you take sample form ADC and send it over softserial . load the following code to arduino.

int id=99;
#include <SoftwareSerial.h>

SoftwareSerial sw(2, 3); // RX, TX

void setup() {
 Serial.println("Interfacfing arduino with nodemcu");

void loop() {
 Serial.println("Sending data to nodemcu");
 int adc=analogRead(A0);
 Serial.print(id);//sensor id
 sw.print(id);//sensor id
Arduino code

Arduino code

NodeMCU code:

On nodemcu you need to load 4 files, all are available on github.


config.lua (edit your wifi,MQTT broker details here)



Load all these three files to nodemcu. To test the setup, send a message from console , it should appear in your MQTT channel.

Testing MQTT Connection

Testing MQTT Connection

MQTT messages

MQTT messages

Once you verified that the nodemcu is able to connect to broker and send messages, you can connect Arduino to nodemcu. First you should close the serial port connection to nodemcu , then Connect Arduino SoftSerial Tx to NodeMCU Rx (through level shifter).




Add a Comment

Your email address will not be published. Required fields are marked *