Categories
ESP8266

Rotation Sensor Controlling an LED on ESP12E NodeMCU in Arduino IDE

Integrating a Rotation Sensor to an ESP12E ESP8266 NodeMCU Microcontroller to control the intensity of an LED light. Written in Arduino IDE.

You will need:

  • Install Arduino IDE on your computer
  • 1 ESP12E ESP 8266 Development Board
    • Or any other Development board that works with Arduino IDE
  • 1 MicroUsb to USB cable to connect to microcontroller
  • 5 Female-To-Female Jumper cables (or any other combination on breadboard)
  • 1 LED (any color)
  • 1 Rotation Sensor

Not sure what an ESP8266 is? Check out my post What is ESP8266? How is it useful for IoT Prototypes?

Double check the PINOUT of the microcontroller you are using.

Double check the output of the Rotary sensor. Make sure it’s going into an ADC Pin!

Arduino IDE code below

// src code: joebhullar.com - "Rotation Sensor Controlling an LED on ESP12E NodeMCU in Arduino IDE"

int brightness = 0;    
const int led = D2;
const int analogRotationSensor=A0;

// the setup function runs once when you press reset or power the board
void setup() {
  pinMode(led, OUTPUT);
  Serial.begin(115200); // open the serial port at 115200 bps:
}

// the loop function runs over and over again forever
void loop() {
        brightness = analogRead(analogRotationSensor); //use D1 to read the electrical signal
       
       brightness = map(brightness, 5, 1024, 0, 255);
       
          Serial.print("Brightness: ");  // prints a label
          Serial.print(brightness);
          Serial.print("\n");       

     // set the brightness of led:

       analogWrite(led, brightness);      
       delay(10);                        
}

Got a question or comment? Send me a msg in the comment section below:

2 replies on “Rotation Sensor Controlling an LED on ESP12E NodeMCU in Arduino IDE”

Comments are closed.