Posted on Leave a comment

How to use attachInterrupt() in Arduino IDE to toggle an LED

Arduino, with its user-friendly environment and a vast array of libraries, opens up a world of possibilities for electronic enthusiasts and hobbyists. One of the key features that makes Arduino a versatile platform is the ability to use interrupts. In this blog post, we will explore the use of attachInterrupt() in the Arduino IDE to toggle an LED with the press of a button.

Understanding attachInterrupt()

The attachInterrupt() function in Arduino is a powerful tool that allows you to execute a specified function (interrupt service routine or ISR) when a certain condition occurs on a digital pin. This capability is particularly useful for handling external events without constantly polling the pin in the main loop, thus improving efficiency and responsiveness.

For more documentation visit the lin below.

https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/

Components Required

Before we delve into the code, let’s gather the necessary components for this project:

  1. Arduino board (e.g., Arduino Uno)
  2. LED
  3. Resistor (220-330 ohms)
  4. Push button
  5. Jumper wires

Wiring the Circuit

Connect the components as follows:

  • Connect the longer leg of the LED (anode) to a current-limiting resistor (220-330 ohms) then the other end of the resistor to pin A2 on the Arduino.
  • Connect the shorter leg of the LED (cathode) GND on the Arduino.
  • Connect one side of the push button to pin 2 on the Arduino.
  • Connect the other side of the push button to the GND on the Arduino.

The Arduino Sketch

Now, let’s dive into the Arduino sketch that utilizes attachInterrupt() to toggle the LED state when the button is pressed.


int led=A2;
int button=2;
volatile byte state = LOW;

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(button, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(2),blink,CHANGE);
}

void blink()
{
  state = !state;
}

void loop()
{
  digitalWrite(led, !state);
}

Breaking Down the Code

  • We define led and button as the respective pin numbers for the LED and the push button.
  • volatile byte state is a variable that holds the state of the LED, and it’s marked as volatile to indicate that it can be modified in an ISR.
  • In the setup() function, we set the LED pin and button pin modes. Additionally, we attach an interrupt to the button using attachInterrupt(), specifying the function blink to be executed on a state change (CHANGE) of the button pin.
  • The blink() function toggles the state variable when the interrupt is triggered.
  • In the loop() function, we continuously update the LED state based on the value of the state variable.
Posted on Leave a comment

How to setup ESP32 on Arduino IDE

Prerequisites: Before we dive into the setup process, there are a few things you’ll need to gather:

  1. An ESP32 development board: You can find numerous options available online, including the ESP32 DevKitC, NodeMCU-32S, and Wemos D1 R32, among others.
  2. A USB cable: Ensure you have a suitable USB cable to connect your ESP32 board to your computer.
  3. Arduino IDE: Download and install the latest version of the Arduino IDE from the official Arduino website (https://www.arduino.cc/en/software). The IDE provides an easy-to-use interface for writing and uploading code to your ESP32.

Now that you have everything you need, let’s get started with the setup process.

Step 1: Install the ESP32 Board in Arduino IDE:

  1. Open the Arduino IDE.
  2. Go to “File” > “Preferences” to open the Preferences window.
  3. In the “Additional Boards Manager URLs” field, add the following URL:
https://dl.espressif.com/dl/package_esp32_index.json
  1. Click “OK” to save the preferences.
  2. Navigate to “Tools” > “Board” > “Boards Manager.”
  3. In the Boards Manager window, search for “ESP32” and select the “esp32” package by Espressif Systems.
  4. Click the “Install” button to begin the installation process.
  5. Once the installation is complete, close the Boards Manager window.

Step 2: Select the ESP32 Board:

  1. Connect your ESP32 board to your computer using the USB cable.
  2. In the Arduino IDE, go to “Tools” > “Board” and select your ESP32 board from the list of available options. Choose the appropriate board variant based on the specific ESP32 module you are using.

Step 3: Choose the Correct Port:

  1. Navigate to “Tools” > “Port” and select the port to which your ESP32 board is connected. The port name will vary depending on your operating system.
    • On Windows, it will typically be in the format “COMX” (e.g., COM3, COM4).
    • On macOS, it will usually appear as “/dev/cu.SLAB_USBtoUART” or “/dev/cu.usbserial-XXXX” (X represents some alphanumeric characters).
    • On Linux, it may appear as “/dev/ttyUSBX” or “/dev/ttyACMX” (X represents a number).

Step 4: Upload a Test Sketch:

  1. Open the “Blink” example sketch by going to “File” > “Examples” > “01.Basics” > “Blink.”
  2. Make any necessary modifications to the sketch if required.
  3. Click on the “Upload” button (the rightward-pointing arrow) to compile and upload the sketch to your ESP32 board.
  4. Wait for the upload process to complete.
  5. Once the upload is successful, you should see the onboard LED blinking at a regular interval.

Congratulations! You have successfully set up your ESP32 board on the Arduino IDE. You are now ready to unleash the power of the ESP32 and start

Troubleshoot

ImportError: No module named serial in Linux

Traceback (most recent call last):
  File "/home/abhay/.arduino15/packages/esp32/tools/esptool_py/3.0.0/esptool.py", line 38, in <module>
    import serial
ImportError: No module named serial

exit status 1

Compilation error: exit status 1

here’s an alternative solution you can try:

  1. Install pySerial via apt-get:
  • Open a terminal.
  • Run the following command to install pySerial using apt-get:
sudo apt-get install python-serial
  1. Restart Arduino IDE: Close the Arduino IDE completely and then open it again.
  2. Upload the code to the ESP32 board: Try uploading your code to the ESP32 board once again.

By installing python-serial using apt-get, you should be able to resolve the missing serial module error. If you still encounter any issues, please provide any error messages or specific problems you’re facing, along with any additional details about your setup.

Posted on Leave a comment

How to setup NodeMCU on Arduino IDE

NodeMCU is an open-source firmware and development board that is based on the ESP8266 Wi-Fi module. It is a popular platform for Internet of Things (IoT) projects due to its low cost, ease of use, and versatility. In this article, we will discuss how to set up NodeMCU on Arduino IDE.

Step 1: Install Arduino IDE
The first step is to install the Arduino IDE on your computer. You can download the latest version of Arduino IDE from the official website https://www.arduino.cc/en/software. Choose the appropriate version for your operating system and follow the installation instructions.

Step 2: Install ESP8266 Board Manager
After installing the Arduino IDE, you need to install the ESP8266 board manager. This is necessary to add the support for NodeMCU to the Arduino IDE.

To do this, open the Arduino IDE and go to File > Preferences. In the Additional Boards Manager URLs field, paste the following URL:

http://arduino.esp8266.com/stable/package_esp8266com_index.json

Click OK and go to Tools > Board > Boards Manager. In the search box, type “esp8266” and select the “esp8266” board by ESP8266 Community. Click Install.

Step 3: Select the NodeMCU Board
After the installation of the ESP8266 board manager is complete, go to Tools > Board and select “NodeMCU 1.0 (ESP-12E Module)”.

Step 4: Select the Port
Next, you need to select the port to which the NodeMCU is connected. Go to Tools > Port and select the appropriate port.

Step 5: Upload Your Sketch
Now, you are ready to upload your sketch to the NodeMCU. In the Arduino IDE, click on File > New to create a new sketch. Write your code and then click on the Upload button to upload the sketch to the NodeMCU.

Step 6: Verify Upload
Once the upload is complete, you can verify that the sketch has been successfully uploaded by opening the Serial Monitor. Go to Tools > Serial Monitor and set the baud rate to 115200. If everything is working properly, you should see the output from your sketch in the Serial Monitor.