Node MCU - Lab Manual-2-16
Node MCU - Lab Manual-2-16
0 Installed
Before proceeding make sure you have Arduino IDE 2.0 installed in your computer.
Go to the Arduino website and download the version for your operating system.
▪ Windows: run the file downloaded and follow the instructions in the installation guide.
▪ Mac OS X: copy the downloaded file into your application folder.
▪ Linux: extract the downloaded file, and open the arduino-ide file that will launch the IDE.
To install the ESP8266 board in your Arduino IDE, follow these next instructions:
1. In your Arduino IDE 2.0, go to File > Preferences.
2. Copy and paste the following line to the Additional Boards Manager URLs field.
http://arduino.esp8266.com/stable/package_esp8266com_index.json
Adding ESP8266 Board Manager
http://arduino.esp8266.com/stable/package_esp8266com_index.json
Now open the tools in that select Board: “Arduino/Genuino Uno” and click on the Boards Manager as
shown in the figure
The Boards Manager window opens, scroll the window page to bottom till you see the module with the
name ESP8266. Once we get it, select that module and select version and click on the Install button. When
it is installed it shows Installed in the module as shown in the figure and then closes the window.
Selecting ESP8266 Arduino Board
To run the esp8266 with Arduino we have to select the Board: “Arduino/Genuino Uno” and then
change it to NodeMCU 1.0 (ESP-12E Module) or other esp8266 modules depending on what you have
to select and select port, Upload the program.
EXPERIMENT 1:
CIRCUIT DIAGRAM:
CODE:
int led = D1; // LED pin
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
digitalWrite(led, HIGH); delay(1000);
digitalWrite(led, LOW); delay(1000);
}
CONNECT C6 TO C13
EXPERIMENT 2:
CIRCUIT DIAGRAM
CODE:
CIRCUIT DIAGRAM:
CODE:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
#define DHTPIN D4 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay();
display.setTextColor(WHITE);
}
void loop() { delay(5000);
float t = dht.readTemperature();
float h = dht.readHumidity();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
}
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0,0);
display.print("Temperature: ");
display.setTextSize(2);
display.setCursor(0,10);
display.print(t);
display.print(" ");
display.setTextSize(1);
display.cp437(true);
display.write(167);
display.setTextSize(2);
display.print("C");
display.setTextSize(1);
display.setCursor(0, 35);
display.print("Humidity: ");
display.setTextSize(2);
display.setCursor(0, 45);
display.print(h);
display.print(" %");
display.display();
}
CONNECT C5 TO C8 & C3 TO C10
EXPERIMENT 4:
CIRCUIT DIAGRAM:
CODE:
const int trigPin = D5; //trig
const int echoPin = D4; //Echo
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(115200); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
delay(2000);
}
CONNECT C3 TO C9
EXPERIMENT 5:
CIRCUIT DIAGRAM:
CODE:
int light_pin = D4;
void setup() {
pinMode(light_pin, INPUT);
Serial.begin(115200);
}
void loop() {
int light_data = digitalRead(light_pin);
if(light_data==0){
Serial.println("Light Detected!");
}
else{
Serial.println("Light not Detected!");
}
delay(100);
}
CIRCUIT DIAGRAM:
CODE:
void loop() {
int SM_value = analogRead(SM_pin);
Serial.print("SOIL MOISTURE Value : ");
Serial.println(SM_value);
ThingSpeak.writeField(myChannelNumber, 3, SM_value, myWriteAPIKey);
Serial.println("data sent to cloud");
delay(1000);
}