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. |