LEDs have two legs, an anode (longer leg, positive) and a cathode (shorter leg, negative). Connect the anode to a current-limiting resistor and the other end of the resistor to the microcontroller’s output pin. Connect the cathode directly to the microcontroller’s ground (GND) pin. Interfacing an LED with an ESP8266 using the Arduino core is quite similar to the basic Arduino example. The ESP8266 Arduino core provides a convenient platform for programming ESP8266 modules with the Arduino IDE. Here’s a simple example of “interfacing an LED with an ESP8266 using an Arduino-core”ESP8266 Arduino-Core Interface – LED”:
Archives
ESP8266 NodeMCU Interface – DSB1820
Code
local ow_pin = 1 -- gpio-02 ds18b20.setup(ow_pin) -- read all sensors and print all measurement results ds18b20.read( function(ind,rom,res,temp,tdec,par) print(ind,string.format("%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",string.match(rom,"(%d+):(%d+):(%d+):(%d+):(%d+):(%d+):(%d+):(%d+)")),res,temp,tdec,par) print("Temp : "..temp) end,{});
ESP8266 NodeMCU Interface – ADXL345
The ESP8266 NodeMCU is a popular development board based on the ESP8266 microcontroller, which offers built-in Wi-Fi capabilities, making it ideal for Internet of Things (IoT) projects. In this project, we’ll explore how to interface the ESP8266 NodeMCU with the ADXL345 accelerometer sensor.
Read more: ESP8266 NodeMCU Interface – ADXL345ADXL345
The ADXL345 is a small, thin, ultralow power, 3-axis accelerometer with high resolution (13-bit) measurement at up to ±16 g. It’s suitable for various applications, including tilt sensing, motion detection, and vibration monitoring.
By combining the ESP8266 NodeMCU with the ADXL345 sensor, we can create IoT applications that can monitor and analyze motion or vibrations remotely over Wi-Fi. This interface allows us to gather data from the accelerometer sensor and transmit it to a server, cloud platform, or display it on a web page.
Throughout this project, we’ll cover the hardware setup, including connecting the ADXL345 sensor to the ESP8266 NodeMCU, and the software implementation, which involves programming the ESP8266 NodeMCU to communicate with the ADXL345 sensor and transmit the data. With this interface, you’ll be able to leverage the power of the ESP8266 NodeMCU and the capabilities of the ADXL345 sensor to create versatile IoT applications for motion sensing and monitoring.
Code
Make sure you have the necessary libraries and setup to run this code on your hardware.
-- www.ArunEworld.com sda = 2 -- GPIO-4 scl = 1 -- GPIO-5 print("ArunEworld") -- Initialize the ADXL345 sensor adxl345.init(sda, scl) -- Delay for 3000000 microseconds (3 seconds) tmr.delay(3000000) -- Read data from the ADXL345 sensor print(adxl345.read()) -- Define a function to read accelerometer data function ADXL_read() -- Read accelerometer data local x, y, z = adxl345.read() -- Print accelerometer data print("X-axis:", x) print("Y-axis:", y) print("Z-axis:", z) -- Alternatively, you can print all axes at once using the following line: -- print(string.format("X = %d, Y = %d, Z = %d", x, y, z)) end -- Set up a timer to call the ADXL_read function every 1000 milliseconds (1 second) tmr.alarm(0, 1000, tmr.ALARM_AUTO, ADXL_read)
Code Explanation
Line | Code | Explanation |
---|---|---|
1 | sda = 2 | Assigns pin 2 to the variable sda , representing the Serial Data (SDA) pin. |
2 | scl = 1 | Assigns pin 1 to the variable scl , representing the Serial Clock (SCL) pin. |
4 | print("ArunEworld") | Prints the message “ArunEworld” to the console. |
7 | adxl345.init(sda, scl) | Initializes the ADXL345 sensor with the SDA and SCL pins defined earlier. |
10 | tmr.delay(3000000) | Delays the execution of the program for 3 seconds (3000000 microseconds). |
13 | print(adxl345.read()) | Reads data from the ADXL345 sensor and prints it to the console. |
16 | function ADXL_read() ... end | Defines a function named ADXL_read to read accelerometer data from the ADXL345 sensor. |
19 | tmr.alarm(0, 1000, tmr.ALARM_AUTO, ADXL_read) | Sets up a timer to call the ADXL_read function every 1000 milliseconds (1 second) repeatedly. |
NEXT
ESP8266 Mongoose-OS Interface – LED
To turn on the led you need to load the gpio_api.js file.
LED Turn ON/OFF
Note : This below example was tested mongoose os with java script app demo-js in ESP8266 NodeMCU Dev Board(ESP-12E)
- This below example turn on the LED GPIO-4 (NodeMCU Dev Board digital pin 2)
- Set the GPIO pin mode as OUPUT uisng gpio set mode function ex: GPIO.set_mode(led, GPIO.MODE_OUTPUT)
- Turn off the LED call the gpio write function with low-0 ex : GPIO.write(led, 0) .
- Turn on the LED call the gpio write function with low-1 ex : GPIO.write(led, 1) .
Code
load('api_gpio.js'); // Configure LED let led = 4; //lde pin GPIO04 GPIO.set_mode(led, GPIO.MODE_OUTPUT); GPIO.write(led,1);
LED turn ON/OFF using button
- Use Boot button(GPIO-0)
- The example code used blink in-build LED, It’s connected to GPIO-10.
- You can change any other pin Ex : led = 4; // Get LED GPIO-04 pin;
- Note : Code 1 and Two are same application and functionality. Main difference is calling function.
Code 1 :
- Used GPIO-0 pin (Boot button in NodeMCU Dev Board), as interrupt and used Button interrupt handler function.
// http://www.ArunEworld.com/Embedded/ESPressif/ESP8266/ESP8266_Mongoose-os/ // Tested By : Arun(20170430) // Example Name : AEW_LED-Blink_Using_Button.js // Firmware : Mongoose OS for ESP32 //*******************************// // This example demonstrates how to react on a button press by toggling LED. // // To try this example, // 1. Download <code>{{EJS1}}</code> tool from https://mongoose-os.com/software.html // 2. Run <code>{{EJS2}}</code> tool and install Mongoose OS // 3. In the UI, navigate to the <code>{{EJS3}}</code> tab and load this example // Load Mongoose OS API load('api_gpio.js'); // Configure LED let led = ffi('int get_led_gpio_pin()')(); // Get built-in LED GPIO pin GPIO.set_mode(led, GPIO.MODE_OUTPUT); let pin = 0; // GPIO 0 is typically a 'Flash' button GPIO.set_button_handler(pin, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 200, function(x) { let value = GPIO.toggle(led); print('Button press, pin:', x, 'LED pin:', led, ' Value: ', value); }, true); print('Flash button is configured on GPIO pin ', pin); print('Press the flash button now!');
Code 2:
- In this code i was used GPIO-0 pin (Boot button in NodeMCU dev Kit) as interrupt and used interrupt handler function.
- If your using interruot handler function then you need o enable the interrupt using GPIO.enable_int(pin) function
load('api_gpio.js'); GPIO.set_mode(4, 1); //GPIO.write(10, 0); GPIO.set_int_handler(0, GPIO.INT_EDGE_NEG, function(pin) { print('Pin', 0, 'got interrupt'); GPIO.toggle(4); }, null); GPIO.enable_int(0);