✦Best Coloring ideas Insights

I2C to PWM Script for NodeMCU: Convert & Control

By

Are you looking to control Light Emitting Diodes (LEDs) or other actuators with variable intensity using an ESP8266 NodeMCU? You can achieve this by implementing an I2C to PWM script. But what exactly is this and how does it work? Let's explore the fascinating world of I2C and PWM, and learn how to make them work together on your NodeMCU.

Before diving into the script, let's understand the key players here: I2C (Inter-Integrated Circuit) and PWM (Pulse Width Modulation). I2C is a two-wire serial bus used for communication between short-distance, clocked integrated circuits. PWM, on the other hand, is a modulation technique that controls the voltage or current to the load. By varying the duty cycle, you can control the intensity of LEDs or the speed of motors, among other things.

Understanding I2C on NodeMCU

I2C is a simple yet powerful communication protocol that allows multiple devices to communicate over two wires. On the NodeMCU, I2C is interfaced through GPIO pins D1 (SCL) and D2 (SDA). The Wire library in Arduino IDE enables us to interact with I2C devices. Let's explore its essential aspects before moving on to PWM.

Interfacing NodeMCU with LM35 temperature sensor
Interfacing NodeMCU with LM35 temperature sensor
How to make a WIFI controlled car using Nodemcu and Blynk app - Step by step instructions - SriTu Hobby
How to make a WIFI controlled car using Nodemcu and Blynk app - Step by step instructions - SriTu Hobby
Esp8266 12e With Arduino IDE With Solution+Examples
Esp8266 12e With Arduino IDE With Solution+Examples

In the following sections, we'll discuss two crucial subtopics: I2C addresses and I2C communication. Understanding these will lay a solid foundation for implementing I2C to PWM on your NodeMCU.

I2C Addresses

Each I2C device has a unique address, which is a 7-bit number. The address is used for data communication between the master (here, the NodeMCU) and the slaves. An I2C address is assigned at the time of device hardwiring and is often mentioned in the device's datasheet. For example, the I2C address of an MPU6050 gyroscope is 0x68.

ESP8285 Development Board Nodemcu-M Based On ESP-M2
ESP8285 Development Board Nodemcu-M Based On ESP-M2
How to set up the new Blynk app step by step | Nodemcu ESP8266 with Blynk app
How to set up the new Blynk app step by step | Nodemcu ESP8266 with Blynk app
script ideia. ✮
script ideia. ✮

There are 128 possible addresses on the I2C bus, meaning you can connect up to 128 devices. However, this number decreases to 112 if:

  • You're using 10-bit addressing (which is seldom the case).
  • The address 0x00 is used by the master (the NodeMCU).

I2C Communication

I2C communication involves a master and one or more slave devices. The master initiates communication by pulling the clock (SCL) line low and then high. It sends a start signal, followed by the slave address and a read/write bit. If the address matches, the slave device responds. Then, the master sends data or receives it from the slave.

Here's a simplified I2C communication sequence:

200 WPM typing journal
200 WPM typing journal
an image of a man looking down at his cell phone screen with text on it
an image of a man looking down at his cell phone screen with text on it
Using N8n WhatsApp Controlled Home Automation Using ESP32
Using N8n WhatsApp Controlled Home Automation Using ESP32
the text on the screen is clearly visible for us to see in this screenshot
the text on the screen is clearly visible for us to see in this screenshot
wondream ‧₊˚☁
wondream ‧₊˚☁
magical girl script template || shifting
magical girl script template || shifting
  1. Master: Start
  2. Master: Slave address | Read/Write (e.g., 0x34 | Write)
  3. Slave: Acknowledge
  4. Master: Data (e.g., 0x12)
  5. Slave: Acknowledge
  6. Master: Stop

Understanding PWM on NodeMCU

PWM is like a heartbeat for electronic loads. It generates a signal whose width varies based on the duty cycle. The duty cycle is the percentage of time that the signal is active within one cycle. Duty cycle magnitudes range from 0% (always off) to 100% (always on). For LEDs, lower duty cycles dim the light; higher cycles brighten it.

The NodeMCU has PWM-capable GPIO pins that can generate these varying width pulses. Pins 5, 4, 0, 2, 14, 12, and 13 are some examples. To generate PWM signals, we use the following functions from the Arduino framework:

  • analogWrite(pin, value) - Sets the duty cycle for a given pin. The value ranges from 0 (always off) to 1023 (always on).
  • I2C_PWM între rdu sppiD, address, pin, dutycycle) - Sends PWM data via I2C to the specified slave address. The dutycycle value is between 0 and 100.

Setting up I2C to PWM on NodeMCU

To implement I2C to PWM on NodeMCU, you'll need an I2C-compatible device with PWM functionalities, like the PCA9685 LED driver. This device allows you to control up to 16 LEDs/actuators individually and generates PWM signals for each output.

GitHub - Nama/SuckControl: Automatic speed control of any fan depending on any temperature sensor.
GitHub - Nama/SuckControl: Automatic speed control of any fan depending on any temperature sensor.
In-Depth Guide to the Top 10 NLP Software Tools Of 2026
In-Depth Guide to the Top 10 NLP Software Tools Of 2026

Here's a basic setup wiring for PCA9685:

  • Connect PCA9685's VCC and GND to the NodeMCU's 3V3 and GND, respectively.
  • Connect PCA9685's SDA to NodeMCU's D2 and SCL to D1.
  • Connect LED/Motor to PCA9685's output pin.

Scripting I2C to PWM on NodeMCU

The following is a step-by-step script for I2C to PWM using the PCA9685 driver and NodeMCU:

  1. Include necessary libraries at the top of your code: #include <Wire.h> and #include <Adafruit_PWMServoDriver.h>.
  2. Initialize the PCA9685 driver with the appropriate I2C address: Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(); pwm.begin(0x40);
  3. Control PWM intensity: pwm.setPWM(outputPin, 0, dutycycleValue);
  4. Don't forget to call the loop() function periodically to refresh the outputs: void loop() {}

Let's recap. We've discussed I2C, what it is, and how to use it on the NodeMCU. We also explored PWM, its functionality, and how you can generate PWM signals on the NodeMCU. Finally, we combined these two fantastic technologies to generate I2C-controlled PWM signals on the NodeMCU. With this knowledge, you're now ready to light up your LEDs or drive your motors with variable intensity!