<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Samreen Ansari</title>
    <description>The latest articles on DEV Community by Samreen Ansari (@samdaqueen).</description>
    <link>https://dev.to/samdaqueen</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1308995%2Fd46bece3-1746-4731-a144-88fd0686aef3.jpeg</url>
      <title>DEV Community: Samreen Ansari</title>
      <link>https://dev.to/samdaqueen</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/samdaqueen"/>
    <language>en</language>
    <item>
      <title>Communication b/w Arduino and ESP8266📶</title>
      <dc:creator>Samreen Ansari</dc:creator>
      <pubDate>Mon, 04 Mar 2024 18:00:00 +0000</pubDate>
      <link>https://dev.to/samdaqueen/communication-bw-arduino-and-esp8266-289n</link>
      <guid>https://dev.to/samdaqueen/communication-bw-arduino-and-esp8266-289n</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Last time, we set up VSC for Arduino development and wrote code for making the LED on the Arduino blink. Today, we will see how to communicate between the Arduino and the ESP8266 Wi-FI module.&lt;br&gt;
&lt;em&gt;&lt;strong&gt;Why do we need to do this?&lt;/strong&gt;&lt;/em&gt; In our Arduino car project, we'll perform computations on the computer and send instructions wirelessly to the Arduino via Wi-Fi. Since conventional Arduinos lack built-in Wi-Fi capability, we rely on external Wi-Fi modules to facilitate this communication.&lt;/p&gt;
&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;p&gt; 1. Introduction&lt;br&gt;
 2. Table of Contents&lt;br&gt;
 3. The Components&lt;br&gt;
 4. Communication Protocols&lt;br&gt;
      4.1 Synchronous vs. Asynchronous&lt;br&gt;
      4.2 Serial vs. Parallel&lt;br&gt;
      4.3 Common Protocols&lt;br&gt;
 5. Communication between the Arduino and ESP8266&lt;br&gt;
      5.1 The ESP8266 Wi-Fi Module&lt;br&gt;
      5.2 Getting the ESP8266 ready to connect to the Arduino&lt;br&gt;
      5.3 Wiring&lt;br&gt;
      5.4 Installing the board manager for ESP8266&lt;br&gt;
      5.5 Implementing I2C Communication in Code&lt;br&gt;
            5.5.1 Creating the directory structure&lt;br&gt;
            5.5.2 Writing the code&lt;br&gt;
            5.5.3 Uploading the code to the devices&lt;br&gt;
            5.5.4 Monitoring the output&lt;br&gt;
            5.5.5 Troubleshooting&lt;br&gt;
 6. What's Next&lt;br&gt;
 7. Resources&lt;/p&gt;
&lt;h2&gt;
  
  
  The Components
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Arduino MEGA2560 board&lt;/li&gt;
&lt;li&gt;USB Cable Type-A/C to Type-B&lt;/li&gt;
&lt;li&gt;ESP8266 D1 Mini Wi-Fi Module&lt;/li&gt;
&lt;li&gt;USB Cable Type-A/C to Type-B&lt;/li&gt;
&lt;li&gt;Breadboard&lt;/li&gt;
&lt;li&gt;2 Breadboard jumper wires&lt;/li&gt;
&lt;li&gt;Soldering iron and wire&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhu4jdg4o4fvpwekhw96x.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhu4jdg4o4fvpwekhw96x.jpg" alt="Components" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Communication Protocols
&lt;/h2&gt;

&lt;p&gt;Embedded electronic components use communication protocols to share information between them. &lt;br&gt;
Communication protocols are of different kinds (Read more at &lt;a href="https://learn.sparkfun.com/tutorials/serial-communication/all"&gt;Sparkfun&lt;/a&gt;):&lt;/p&gt;
&lt;h3&gt;
  
  
  1. Synchronous vs. Asynchronous
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Synchronous communication&lt;/em&gt; involves synchronized timing between the sender and receiver. Both parties must agree on the timing of data transmission. It involves a clock signal along with the data signal.&lt;br&gt;
&lt;em&gt;Asynchronous communication&lt;/em&gt; does not rely on synchronized timing. Instead, data is transmitted with start and stop bits, allowing for more flexible timing.&lt;/p&gt;
&lt;h3&gt;
  
  
  2. Serial vs. Parallel
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Serial communication&lt;/em&gt; transmits data one bit at a time over a single wire or channel. It's simpler and more cost-effective for long-distance communication.&lt;br&gt;
&lt;em&gt;Parallel communication&lt;/em&gt; transmits multiple bits simultaneously over separate wires or channels. It offers higher data transfer rates but requires more wires and is prone to signal degradation over longer distances.&lt;/p&gt;
&lt;h3&gt;
  
  
  3. Common Protocols
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;UART (Universal Asynchronous Receiver-Transmitter)&lt;/em&gt;:
Used for &lt;em&gt;asynchronous serial&lt;/em&gt; communication between devices. Commonly used for simple point-to-point communication.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;SPI (Serial Peripheral Interface)&lt;/em&gt;:
Allows full-duplex &lt;em&gt;synchronous serial&lt;/em&gt; communication between microcontrollers and peripheral devices. Ideal for high-speed communication with short distances.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;I2C (Inter-Integrated Circuit)&lt;/em&gt;:
A &lt;em&gt;synchronous serial&lt;/em&gt; communication protocol that allows communication between multiple devices using only two wires (SDA: serial data and SCL: serial clock). It supports multiple devices connected to the same bus, making it suitable for intra-board communication and communication between different components.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Communication between the Arduino and ESP8266
&lt;/h2&gt;

&lt;p&gt;For this project, I chose &lt;a href="https://learn.sparkfun.com/tutorials/i2c"&gt;I2C communication protocol&lt;/a&gt; due to its:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Simplicity: With I2C, only two wires are needed for communication, simplifying the hardware setup and reducing wiring complexity in our car.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Efficiency: I2C's synchronous nature ensures precise timing and efficient data transfer, making it ideal for real-time communication.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  The ESP8266 WiFi Module
&lt;/h3&gt;

&lt;p&gt;The ESP8266 is a versatile and cost-effective Wi-Fi module that lets microcontrollers like Arduino connect to wireless networks. It acts as a bridge between the Arduino and the internet, allowing the Arduino to communicate with other devices and services over Wi-Fi.&lt;/p&gt;
&lt;h3&gt;
  
  
  Getting the ESP8266 ready to connect to the Arduino
&lt;/h3&gt;

&lt;p&gt;Some ESP8266 modules might already come soldered with the header pins, which let us use the ESP8266 on a breadboard. In my case, it wasn't already soldered. As I had never soldered before, I used the videos below as a reference before soldering:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=Qps9woUGkvI"&gt;Soldering Tutorial for Beginners: Five Easy Steps - YouTube&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=dbh49CqHe9E"&gt;Solder Header Pins onto your Wemos D1 Mini Esp8266 Arduino - YouTube&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;Issues I faced while soldering:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;My solder would not get hot enough. I recommend using a better soldering iron instead of the $10 one I bought lol&lt;/li&gt;
&lt;li&gt;The solder tip kept getting covered by the solidified metal and hence could not reach the point where I wanted to solder&lt;/li&gt;
&lt;li&gt;I accidentally soldered two adjacent points together!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I had a desoldering pump and could remove the incorrect solders after a few tries. Soldering was wayyyy trickier than I expected, but I believe it will be better the next time I try it.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  Wiring
&lt;/h3&gt;

&lt;p&gt;As we chose I2C communication, we only need two wires between the Arduino and ESP8266.&lt;br&gt;
The image below shows the pin layout of the ESP8266 module:&lt;/p&gt;


  &lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbc2epy6wpkik69vanhzw.png" alt="ESP8266 Pin Layout" width="715" height="362"&gt;Image by https://randomnerdtutorials.com/esp8266-pinout-reference-gpios/

  


&lt;p&gt;The Arduino has the SDA and the SCL pins above the reset button.&lt;/p&gt;

&lt;p&gt;Steps to connect:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Connect the Arduino to the computer using Type-B cable&lt;/li&gt;
&lt;li&gt;Connect the ESP8266 to the computer using Type-C cable&lt;/li&gt;
&lt;li&gt;Connect D1 (SCL) on the ESP8266 with the SCL pin on Arduino&lt;/li&gt;
&lt;li&gt;Connect D2 (SDA) on the ESP8266 with the SDA pin on Arduino&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvux7jpkrk5gj7k7p3llj.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvux7jpkrk5gj7k7p3llj.jpg" alt="Wiring" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Installing the board manager for ESP8266
&lt;/h3&gt;

&lt;p&gt;Before the begin writing our code, we need to set up VSC for the ESP8266. You can find similar steps for the Arduino IDE under Resources.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Board Manager: Type &lt;code&gt;Ctrl+Shift+P&lt;/code&gt; to open Command Palette in VSC and then search for &lt;code&gt;Arduino: Board Manager&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F38ct7940dq6gr55ea2ny.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F38ct7940dq6gr55ea2ny.png" alt="Board Manager" width="800" height="179"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Within the Board Manger, search for &lt;code&gt;esp8266&lt;/code&gt; and install the package shown below&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnlxn0awq6a7qiiwg3fvd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnlxn0awq6a7qiiwg3fvd.png" alt="esp8266" width="800" height="472"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Settings.json: Type &lt;code&gt;Ctrl+P&lt;/code&gt; to open a specific file in VSC and then search for &lt;code&gt;settings.json&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foo6icqyjy1py01125nyw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foo6icqyjy1py01125nyw.png" alt="Settings.json" width="800" height="98"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Within &lt;code&gt;settings.json&lt;/code&gt;, add this code at the end of the JSON and then save:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="nl"&gt;"arduino.additionalUrls"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="s2"&gt;"http://arduino.esp8266.com/stable/package_esp8266com_index.json"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvx1acy0bviffjltr8t1z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvx1acy0bviffjltr8t1z.png" alt="Settings ESP" width="800" height="119"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Implementing I2C Communication in Code
&lt;/h3&gt;

&lt;p&gt;In I2C communication, there is typically a primary device and one or more secondary devices. The primary device initiates communication by sending requests or instructions, and the secondary devices respond accordingly. In this case, the ESP8266 will be the primary (as it can't be secondary) and the Arduino will be the secondary.&lt;/p&gt;
&lt;h4&gt;
  
  
  Creating the directory structure
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Create a new directory called &lt;code&gt;arduino-car&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Within the &lt;code&gt;arduino-car&lt;/code&gt; directory, create two new directories called &lt;code&gt;arduino&lt;/code&gt; and &lt;code&gt;esp8266&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Within the &lt;code&gt;arduino&lt;/code&gt; directory:

&lt;ol&gt;
&lt;li&gt;Create a sketch file named &lt;code&gt;arduino.ino&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Create a directory &lt;code&gt;src/Secondary&lt;/code&gt; with new Secondary.h header file and Secondary.cpp implementation file.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;Within the &lt;code&gt;arduino&lt;/code&gt; directory:

&lt;ol&gt;
&lt;li&gt;Create a sketch file named &lt;code&gt;arduino.ino&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Create a directory &lt;code&gt;src/Secondary&lt;/code&gt; with new Secondary.h header file and Secondary.cpp implementation file.
&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn3ol445uppeozl6v56p0.png" alt="Project structure" width="710" height="421"&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;
  
  
  Writing the code
&lt;/h4&gt;

&lt;p&gt;Before we begin, let's discuss the &lt;em&gt;Serial Monitor, baud rate&lt;/em&gt;, and &lt;code&gt;Serial.print()&lt;/code&gt; function. These are essential tools in Arduino development for communicating with your computer and debugging your code.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Serial Monitor:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;It's a tool for monitoring Arduino output, debugging code, and interacting with projects in real-time&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Baud rate&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;This determines the speed of communication in bits per second between Arduino and the Serial Monitor.&lt;/li&gt;
&lt;li&gt;Match the baud rate in your code (&lt;code&gt;Serial.begin()&lt;/code&gt;) with the one in the Serial Monitor to avoid garbled or incorrect data&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Serial.print()&lt;/code&gt;:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;It is used to send data from the Arduino to the Serial Monitor for display.&lt;/li&gt;
&lt;li&gt;Example:
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sensorValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Serial&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Sensor value: "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Serial&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sensorValue&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h5&gt;
  
  
  Primary
&lt;/h5&gt;

&lt;p&gt;First, let us write the code for the primary device - ESP8266. There are comments and docstrings in the code for better understanding.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Primary.h&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;This creates a header file where we declare the functions and the variables&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We use the Wire library for the I2C communication between the modules&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We need the address of the secondary module here which will be defined when initializing the secondary&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We have functions for requesting data from and sending data to secondary&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#ifndef PRIMARY_H
#define PRIMARY_H
&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;Wire.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;Arduino.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="cm"&gt;/**
 * @brief The Primary class functions for the ESP8266 sketch.
 * @author Samreen Ansari
 *
 * This class provides functionality to set up the I2C communication.
 */&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Primary&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="cm"&gt;/**
   * @brief Constructs a new Primary object.
   */&lt;/span&gt;
  &lt;span class="n"&gt;Primary&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="cm"&gt;/**
   * @brief Initializes the I2C communication for sending and receiving data.
   */&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;initialize_i2c&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="cm"&gt;/**
   * @brief Requests data from the secondary module.
   */&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;request_data_from_secondary&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="cm"&gt;/**
   * @brief Sends data to the secondary module.
   *
   * @param data The data to be sent.
   */&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;send_data_to_secondary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

 &lt;span class="nl"&gt;private:&lt;/span&gt;
  &lt;span class="cm"&gt;/** The I2C address of the secondary module. */&lt;/span&gt;
  &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;SECONDARY_ADDRESS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="cp"&gt;#endif  // PRIMARY_H
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;&lt;em&gt;Primary.cpp&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Here we implement our header file &lt;code&gt;Primary.h&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Since this is our primary module, we do not need to specify the address when using &lt;code&gt;Wire.begin()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * @file Primary.cpp
 * @brief Implementation file for the Primary class.
 * @author Samreen Ansari
 */&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"Primary.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="n"&gt;Primary&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Primary&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;Primary&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;initialize_i2c&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Begin the I2C communication (primary does not need to specify address).&lt;/span&gt;
  &lt;span class="n"&gt;Wire&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="n"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// add delay to give time for initialization&lt;/span&gt;
  &lt;span class="n"&gt;Serial&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"I2C Primary Initialized"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;Primary&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;request_data_from_secondary&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Request 21 bytes of data from the secondary device and print it.&lt;/span&gt;
  &lt;span class="n"&gt;Wire&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;requestFrom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SECONDARY_ADDRESS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// While communication is available,&lt;/span&gt;
  &lt;span class="c1"&gt;// keep reading each character and printing&lt;/span&gt;
  &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Wire&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;available&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Wire&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;read&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;Serial&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;Serial&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;println&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;Primary&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;send_data_to_secondary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Send data to the secondary device.&lt;/span&gt;
  &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strlen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;Wire&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;beginTransmission&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SECONDARY_ADDRESS&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// begin sending data&lt;/span&gt;
  &lt;span class="n"&gt;Wire&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;write&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="kt"&gt;uint8_t&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// write data of len in format uint8&lt;/span&gt;
  &lt;span class="n"&gt;Wire&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;endTransmission&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;&lt;em&gt;esp8266.ino&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the sketch file, we use the functions from Primary and request data from secondary, as well as send some data to secondary.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Notice the baud rate of 115200 here&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * @file esp8266.ino
 * @brief Arduino code for communication with the WIFi module.
 * @author Samreen Ansari
 *
 * This code initializes a I2C Communication between the Arduino and the
 * ESP8266. The ESP8266 sends data to the Arduino and requests data from the
 * Arduino.
 */&lt;/span&gt;

&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"src/primary/Primary.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="n"&gt;Primary&lt;/span&gt; &lt;span class="n"&gt;primaryDevice&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// create an object of the primary class&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * @brief Initializes the ESP8266 primary WiFi module.
 *
 * This function initializes the I2C communication with the secondary device.
 */&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;Serial&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;115200&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Set the baud rate to monitor serial communication&lt;/span&gt;
  &lt;span class="n"&gt;primaryDevice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;initialize_i2c&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * @brief The main loop of the ESP8266 primary WiFi module.
 *
 * This function is called repeatedly send data to the secondary device,
 * and request data from the device.
 */&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;primaryDevice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;request_data_from_secondary&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// request data&lt;/span&gt;
  &lt;span class="n"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// wait for 1 second&lt;/span&gt;
  &lt;span class="n"&gt;primaryDevice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;send_data_to_secondary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"from primary to secondary!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;send&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;
  &lt;span class="n"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// wait for 1 second&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h5&gt;
  
  
  Secondary
&lt;/h5&gt;

&lt;p&gt;Now, we look at the code for the secondary device - Arduino MEGA2560.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Secondary.h&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;This creates a header file where we declare the functions and the variables&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We have functions for responding to requests and receiving data from primary&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#ifndef SECONDARY_H
#define SECONDARY_H
&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;Wire.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;Arduino.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="cm"&gt;/**
 * @brief The Secondary class functions for the Arduino sketch.
 * @author Samreen Ansari
 *
 * This class provides functionality to set up the I2C communication.
 */&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Secondary&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="cm"&gt;/**
   * @brief Constructs a new Secondary object.
   */&lt;/span&gt;
  &lt;span class="n"&gt;Secondary&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="cm"&gt;/**
   * @brief Initializes the I2C communication for sending and receiving data.
   */&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;initialize_i2c&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="cm"&gt;/**
   * @brief Sends data to the primary device when requested.
   */&lt;/span&gt;
  &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;respond_data_to_primary&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="cm"&gt;/**
   * @brief Receives data from the primary device.
   *
   * @param byteCount The number of bytes to receive.
   */&lt;/span&gt;
  &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;receive_data_from_primary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;byteCount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

 &lt;span class="nl"&gt;private:&lt;/span&gt;
  &lt;span class="cm"&gt;/** The I2C address of the Arduino. */&lt;/span&gt;
  &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;SECONDARY_ADDRESS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="cp"&gt;#endif
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;&lt;em&gt;Secondary.cpp&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Here we implement our header file &lt;code&gt;Secondary.h&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Since this is our secondary module, we need to specify the address here in &lt;code&gt;Wire.begin()&lt;/code&gt;, we use 8&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We bind the functions for responding to request and receiving data using the &lt;code&gt;Wire.onRequest()&lt;/code&gt; and &lt;code&gt;Wire.onReceive()&lt;/code&gt; functions. We need the functions to be &lt;strong&gt;&lt;em&gt;static&lt;/em&gt;&lt;/strong&gt; in order for this to work&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * @file Secondary.cpp
 * @brief Implementation file for the Secondary class.
 * @author Samreen Ansari
 */&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"Secondary.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="n"&gt;Secondary&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Secondary&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;Secondary&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;initialize_i2c&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Begin the synchronous I2C communication with the specified address.&lt;/span&gt;
  &lt;span class="n"&gt;Wire&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SECONDARY_ADDRESS&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Register the functions to be called when data is requested or received.&lt;/span&gt;
  &lt;span class="n"&gt;Wire&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;onRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;respond_data_to_primary&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;Wire&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;onReceive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;receive_data_from_primary&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="n"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;Serial&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"I2C Secondary Initialized"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;Secondary&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;respond_data_to_primary&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Send a response to the primary device when requested.&lt;/span&gt;
  &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello from secondary!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;messageSize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strlen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// Respond with message of messageSize in format uint8.&lt;/span&gt;
  &lt;span class="n"&gt;Wire&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;write&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;messageSize&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;Secondary&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;receive_data_from_primary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;byteCount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Receive data and print from the primary device whenever sent.&lt;/span&gt;
  &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Wire&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;available&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Wire&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;read&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// read each character and print it&lt;/span&gt;
    &lt;span class="n"&gt;Serial&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;Serial&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;println&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;&lt;em&gt;arduino.ino&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the sketch file, we use the function from Secondary to initialize the I2C on the Arduino.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Notice the different baud rate of 9600 here&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * @file arduino.ino
 * @brief Arduino Sketch for the Arduino Secondary Device
 * @author Samreen Ansari
 *
 * This sketch initializes the Arduino secondary device, sets up the serial
 *communication, and initializes the I2C communication for the secondary device.
 **/&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;Wire.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"src/secondary/Secondary.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="n"&gt;Secondary&lt;/span&gt; &lt;span class="n"&gt;secondaryDevice&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * @brief Initializes the Arduino secondary device.
 *
 * This function sets up the serial communication and initializes the I2C
 * communication for the secondary device.
 */&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;Serial&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;9600&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;secondaryDevice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;initialize_i2c&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * @brief The main loop of the Arduino secondary device.
 *
 * This function is called repeatedly to add a delay of 100 milliseconds.
 */&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  Uploading the code to the devices
&lt;/h4&gt;

&lt;p&gt;Remember to change the Sketch File, Board, and Serial Port from VSC Status Bar and upload the &lt;code&gt;arduino.ino&lt;/code&gt; to the Arduino, and upload the &lt;code&gt;esp8266.ino&lt;/code&gt; to the ESP8366.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;ESP8266:&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg2n5b7025gjnj3o4ut2n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg2n5b7025gjnj3o4ut2n.png" alt="ESP settings" width="695" height="42"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Arduino:&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuar1knzu5rbgcn2vilc6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuar1knzu5rbgcn2vilc6.png" alt="Arduino settings" width="644" height="43"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Monitoring the output
&lt;/h4&gt;

&lt;p&gt;If we had no errors and successfully uploaded the code to the respective devices, we can open Serial Monitor to view the outputs.&lt;/p&gt;

&lt;p&gt;To open it in VSC, press &lt;code&gt;Ctrl+Shift+P&lt;/code&gt; and search for &lt;code&gt;Serial Monitor: Focus on Serial Monitor View&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxk8unhk7p4qc46ku64rp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxk8unhk7p4qc46ku64rp.png" alt="Open serial monitor" width="800" height="129"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here I have two Serial Monitors open, one set to monitor the Arduino on COM8 with baud rate of 9600, and the other to monitor the ESP8266 on COM11 with baud rate 115200.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy1ofd682lbtjxgjqfnzv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy1ofd682lbtjxgjqfnzv.png" alt="Serial Monitors" width="800" height="528"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And now we have two-way communication working between the Arduino and the ESP8266. We can modify what kind of data to send and what to do with the received data.&lt;/p&gt;
&lt;h4&gt;
  
  
  Troubleshooting
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Error Messages&lt;/em&gt;: If you encounter error messages during the upload process, carefully read and understand the error messages displayed in the Arduino IDE. These messages often provide valuable clues about the nature of the problem.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;USB Driver Issues&lt;/em&gt;: Sometimes, driver issues can prevent the Arduino IDE from communicating with the connected devices. Ensure that the necessary USB drivers are installed correctly for your Arduino and ESP8266 modules.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Reset Devices&lt;/em&gt;: If the devices seem unresponsive or stuck during the upload process, try resetting both the Arduino and ESP8266 modules. This can help resolve temporary glitches or communication errors.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Recheck connections&lt;/em&gt;: Ensure that the SCL pin on Arduino is connected to D1 on ESP8266 and the SDA pin is connected to the D2 pin, and that the devices are connected to the computer&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  What's Next
&lt;/h3&gt;

&lt;p&gt;Next time we will see how to communicate between the computer and the Arduino wirelessly.&lt;/p&gt;


&lt;h3&gt;
  
  
  About Me
&lt;/h3&gt;


&lt;div class="ltag__user ltag__user__id__1308995"&gt;
    &lt;a href="/samdaqueen" class="ltag__user__link profile-image-link"&gt;
      &lt;div class="ltag__user__pic"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eLPyww8Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://media.dev.to/cdn-cgi/image/width%3D150%2Cheight%3D150%2Cfit%3Dcover%2Cgravity%3Dauto%2Cformat%3Dauto/https%253A%252F%252Fdev-to-uploads.s3.amazonaws.com%252Fuploads%252Fuser%252Fprofile_image%252F1308995%252Fd46bece3-1746-4731-a144-88fd0686aef3.jpeg" alt="samdaqueen image"&gt;
      &lt;/div&gt;
    &lt;/a&gt;
  &lt;div class="ltag__user__content"&gt;
    &lt;h2&gt;
&lt;a class="ltag__user__link" href="/samdaqueen"&gt;Samreen Ansari&lt;/a&gt;Follow
&lt;/h2&gt;
    &lt;div class="ltag__user__summary"&gt;
      &lt;a class="ltag__user__link" href="/samdaqueen"&gt;Senior Software Engineer @ CrowdDoing | ex-SDE @ Amazon | MS in Computer Science @ NU, Boston&lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;I'm a Software Engineer by profession and tinkerer by passion. I love everything AI, Robotics, and embedded systems. While I may be new to embedded programming, I am super excited about this project and can't wait to watch my car go zooooom! 🚗🔌🤖&lt;/p&gt;

&lt;p&gt;Check out the code on my &lt;a href="https://github.com/SamDaQueen"&gt;GitHub&lt;/a&gt;.&lt;br&gt;
Connect with me on &lt;a href="https://www.linkedin.com/in/samreen-ansari"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Happy tinkering!&lt;/p&gt;




&lt;h4&gt;
  
  
  Resources
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.arduino.cc/"&gt;Arduino Official Website&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.nabto.com/esp8266-for-iot-complete-guide/"&gt;ESP8266 for IoT: A Complete Guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://learn.sparkfun.com/tutorials/serial-communication/all"&gt;Serial Communication by sparkfun&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://learn.sparkfun.com/tutorials/i2c"&gt;I2C by sparkfun&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="[Getting%20Started%20with%20ESP8266%20NodeMCU%20Development%20Board|%20Random%20Nerd%20Tutorials](https://randomnerdtutorials.com/getting-started-with-esp8266-wifi-transceiver-review/)"&gt;Getting Started with ESP8266 NodeMCU Development Board&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/cunchem/I2C_esp8266ToArduinoUno"&gt;I2C between ESP8266 and Arduino UNO&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>arduino</category>
      <category>tutorial</category>
      <category>todayilearned</category>
      <category>ai</category>
    </item>
    <item>
      <title>Setting Up Environment and Components📟</title>
      <dc:creator>Samreen Ansari</dc:creator>
      <pubDate>Sun, 03 Mar 2024 14:00:00 +0000</pubDate>
      <link>https://dev.to/samdaqueen/setting-up-environment-and-components-48o3</link>
      <guid>https://dev.to/samdaqueen/setting-up-environment-and-components-48o3</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this post, we will walk through the steps for setting up our development environment on Windows and introduce the hardware components we'll need for the Arduino self-driving car. We will also make the LED on the Arduino blink.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;p&gt; 1. Introduction&lt;br&gt;
 2. Table of Contents&lt;br&gt;
 3. About Arduino Sketches&lt;br&gt;
 4. The Components&lt;br&gt;
 5. Making the LED on the Arduino blink&lt;br&gt;
       5.1. IDE Setup&lt;br&gt;
       5.2. Connecting the Arduino to VSC&lt;br&gt;
       5.3. Writing our first Arduino Sketch!&lt;br&gt;
 6. What's Next&lt;br&gt;
 7. Resources&lt;/p&gt;
&lt;h2&gt;
  
  
  About Arduino Sketches
&lt;/h2&gt;

&lt;p&gt;Before we begin with the setup, I want to talk a little about Arduino sketches. They are programs written in the Arduino programming language, a simplified version of &lt;em&gt;C&lt;/em&gt; and &lt;em&gt;C++&lt;/em&gt;. These sketches are uploaded to the Arduino microcontrollers. They consist of two main parts: &lt;code&gt;setup()&lt;/code&gt; and &lt;code&gt;loop()&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;setup()&lt;/code&gt; function is called once when the Arduino board is powered on or reset. It is typically used to initialize variables, pin modes, and other settings required for the program.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;loop()&lt;/code&gt; function is executed repeatedly after the &lt;code&gt;setup()&lt;/code&gt; function completes. This is where the main logic of the program is written, controlling the behavior of the Arduino board based on input from sensors, user interactions, or other external factors.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now that we have a basic understanding of Arduino sketches, let's move on to setting up our development environment.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Components
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Arduino MEGA2560 board&lt;/li&gt;
&lt;li&gt;USB Cable Type-A/C to Type-B 
&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9hsadabw78islwrplfgj.jpg" alt="Arduino and Cable" width="800" height="800"&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Making the LED on the Arduino blink
&lt;/h2&gt;

&lt;p&gt;Now let's set up the IDE, connect to Arduino, and write code.&lt;/p&gt;
&lt;h3&gt;
  
  
  IDE Setup
&lt;/h3&gt;

&lt;p&gt;We can use any IDE for the development process. One of the options we can start with is the &lt;a href="https://support.arduino.cc/hc/en-us/articles/360019833020-Download-and-install-Arduino-IDE"&gt;Arduino IDE&lt;/a&gt;, which now has debugging support as well. I prefer using &lt;a href="https://code.visualstudio.com"&gt;Visual Studio Code (VSC)&lt;/a&gt; due to its flexibility, customization, extensions, and integration with Git, and will be explaining the steps on how to set it up.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Download and install the IDE&lt;/strong&gt;&lt;br&gt;
We can use this &lt;a href="https://code.visualstudio.com/download"&gt;link&lt;/a&gt; to download the IDE and then go through the installation steps&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Install extensions&lt;/strong&gt;&lt;br&gt;
Go to the &lt;a href="https://code.visualstudio.com/docs/editor/extension-marketplace#:~:text=You%20can%20browse%20and%20install,on%20the%20VS%20Code%20Marketplace."&gt;Extensions Marketplace&lt;/a&gt;&lt;code&gt;(Ctrl+Shift+X)&lt;/code&gt; in VSC and install these: &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;We should install the &lt;a href="https://marketplace.visualstudio.com/items?itemName=vsciot-vscode.vscode-arduino"&gt;Arduino extension&lt;/a&gt; for writing sketches to the Arduino board through VSC&lt;/li&gt;
&lt;li&gt;For easily viewing the GitHub repository for our code in a graph form, we can use the &lt;a href="https://marketplace.visualstudio.com/items?itemName=mhutchie.git-graph"&gt;Git Graph extension&lt;/a&gt; (optional)&lt;/li&gt;
&lt;li&gt;We can get better syntax highlighting for the code by using the &lt;a href="https://marketplace.visualstudio.com/items?itemName=jeff-hykin.better-cpp-syntax"&gt;Better C++ Syntax extension&lt;/a&gt; (optional)&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Connecting the Arduino to VSC
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Plug in the Arduino to the PC using the cable&lt;/li&gt;
&lt;li&gt;Open Device Manager and look for the connected COM port
&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbjk0rv0vzt7rn5hhaxhu.png" alt="Device Manager" width="548" height="402"&gt;
&lt;/li&gt;
&lt;li&gt;Go to VSC and click on each of these board settings at the bottom of the window
&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5gljkroqb94whn3km0hj.png" alt="Board settings" width="766" height="65"&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Select the Correct Board&lt;/strong&gt;: Click on "Select Board Type" and choose the Arduino board. This ensures that the IDE compiles the code correctly for the specific hardware we are using. Different Arduino boards have different specifications, so it's essential to specify the board.
&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdtzrhw2wrbu5i64jv8hq.png" alt="Board Manager" width="800" height="602"&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Choose the Serial Port&lt;/strong&gt;: Click on "Select Serial Port" and choose the port that was connected to Arduino under Device Manager. This allows the IDE to establish a connection with the Arduino board and upload the compiled code. Selecting the correct serial port ensures that the code can be uploaded to the correct Arduino board.&lt;/li&gt;
&lt;li&gt;If there are multiple sketch files in the directories, make sure the correct one is selected. After the selections, it should look like this:
&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhkb4cgj8z4txipm5cooj.png" alt="VSC Board Settings" width="800" height="425"&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Writing our first Arduino Sketch!
&lt;/h3&gt;

&lt;p&gt;Now that we have the IDE and components ready, we can write a basic sketch to make the LED on the Arduino board blink.&lt;/p&gt;

&lt;p&gt;1. Open VSC to any preferred working directory &lt;br&gt;
2. Create a directory named &lt;code&gt;blink&lt;/code&gt; in the working directory&lt;br&gt;
3. Create a sketch file &lt;code&gt;blink.ino&lt;/code&gt; inside the &lt;code&gt;blink&lt;/code&gt; directory&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Make sure to name the sketch file the same as the directory containing it!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;4. We write the code in our &lt;code&gt;blink.ino&lt;/code&gt; file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;LED_PIN&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;LED_BUILTIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// the output pin number&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;pinMode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;LED_PIN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;OUTPUT&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// set the pin as output&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;digitalWrite&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;LED_PIN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;HIGH&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// turn the LED on&lt;/span&gt;
  &lt;span class="n"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;                 &lt;span class="c1"&gt;// wait for a second&lt;/span&gt;
  &lt;span class="n"&gt;digitalWrite&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;LED_PIN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;LOW&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// turn the LED off&lt;/span&gt;
  &lt;span class="n"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;                 &lt;span class="c1"&gt;// wait for a second&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;5. Code explanation&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;LED_PIN&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;LED_BUILTIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// the output pin number&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;Here we specify the pin number to which our LED is connected. Many Arduino boards have a built-in LED pin in series with a resistor and can be accessed by &lt;code&gt;LED_BUILTIN&lt;/code&gt;. We can use external LEDs connected through wires but must be sure to use it with a resistor to avoid burning up the LED.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;pinMode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;LED_PIN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;OUTPUT&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// set the pin as output&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;setup()&lt;/code&gt; function will run once at the program's start. Here we specify that the LED_PIN is an output pin. &lt;code&gt;pinMode()&lt;/code&gt; sets the given pin component to INPUT, OUTPUT, or INPUT_PULLUP (HIGH if nothing is connected).&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;digitalWrite&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;LED_PIN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;HIGH&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// turn the LED on&lt;/span&gt;
  &lt;span class="n"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;                 &lt;span class="c1"&gt;// wait for a second&lt;/span&gt;
  &lt;span class="n"&gt;digitalWrite&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;LED_PIN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;LOW&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// turn the LED off&lt;/span&gt;
  &lt;span class="n"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;                 &lt;span class="c1"&gt;// wait for a second&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;The code within the &lt;code&gt;loop()&lt;/code&gt; function will continue until we upload some other code or disconnect the Arduino. We are programming the built-in LED to turn on, stay on for 1s, turn off, stay off for 1s, and then go back to the first line of the loop and repeat. We use &lt;code&gt;digitalWrite()&lt;/code&gt; to set the value of the pin as &lt;code&gt;HIGH&lt;/code&gt; or &lt;code&gt;LOW&lt;/code&gt;. &lt;code&gt;delay()&lt;/code&gt; adds the specified millisecond pause to the program.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;6. Run the code&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbq9p9afh0xx5cbuxomur.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbq9p9afh0xx5cbuxomur.jpg" alt="Upload and Verify" width="575" height="236"&gt;&lt;/a&gt;&lt;br&gt;
We can find the Upload and Verify buttons on the top right corner of the window (make sure to be on the sketch file otherwise they will not show up).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Click on Verify to compile the code and check for errors&lt;/li&gt;
&lt;li&gt;Click on Upload to send the code to the connected Arduino&lt;/li&gt;
&lt;li&gt;Watch the LED blink! Yayyy! 🎉&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;7. Make sure to commit and save the code to some version control system (I use GitHub) so we can go back to a stable version if we mess up!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxribukhqg28tnx8klogr.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxribukhqg28tnx8klogr.gif" alt="Blinking LED" width="518" height="518"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt; &lt;br&gt;
There we have it! Our Arduino sketch serves as the first step towards building that car!&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;
&lt;h3&gt;
  
  
  What's Next
&lt;/h3&gt;

&lt;p&gt;Next time, we'll explore how to connect the Arduino to a Wi-Fi module and exchange data, enabling wireless communication with the computer.&lt;/p&gt;


&lt;h3&gt;
  
  
  About Me
&lt;/h3&gt;


&lt;div class="ltag__user ltag__user__id__1308995"&gt;
    &lt;a href="/samdaqueen" class="ltag__user__link profile-image-link"&gt;
      &lt;div class="ltag__user__pic"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eLPyww8Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://media.dev.to/cdn-cgi/image/width%3D150%2Cheight%3D150%2Cfit%3Dcover%2Cgravity%3Dauto%2Cformat%3Dauto/https%253A%252F%252Fdev-to-uploads.s3.amazonaws.com%252Fuploads%252Fuser%252Fprofile_image%252F1308995%252Fd46bece3-1746-4731-a144-88fd0686aef3.jpeg" alt="samdaqueen image"&gt;
      &lt;/div&gt;
    &lt;/a&gt;
  &lt;div class="ltag__user__content"&gt;
    &lt;h2&gt;
&lt;a class="ltag__user__link" href="/samdaqueen"&gt;Samreen Ansari&lt;/a&gt;Follow
&lt;/h2&gt;
    &lt;div class="ltag__user__summary"&gt;
      &lt;a class="ltag__user__link" href="/samdaqueen"&gt;Senior Software Engineer @ CrowdDoing | ex-SDE @ Amazon | MS in Computer Science @ NU, Boston&lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;I'm a Software Engineer by profession and tinkerer by passion. I love everything AI, Robotics, and embedded systems. While I may be new to embedded programming, I am super excited about this project and can't wait to watch my car go zooooom! 🚗🔌🤖&lt;/p&gt;

&lt;p&gt;Check out the code on my &lt;a href="https://github.com/SamDaQueen"&gt;GitHub&lt;/a&gt;.&lt;br&gt;
Connect with me on &lt;a href="https://www.linkedin.com/in/samreen-ansari"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Happy tinkering!&lt;/p&gt;




&lt;h4&gt;
  
  
  Resources
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.arduino.cc/"&gt;Arduino Official Website&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.arduino.cc/reference/en/"&gt;Arduino Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.circuitstate.com/tutorials/how-to-use-vs-code-for-creating-and-uploading-arduino-sketches/"&gt;How to Use VS Code for Creating and Uploading Arduino Sketches&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>arduino</category>
      <category>tutorial</category>
      <category>todayilearned</category>
      <category>ai</category>
    </item>
    <item>
      <title>🔥Wheels and Wires: Building My Arduino Self-Driving Car🚗👩‍💻</title>
      <dc:creator>Samreen Ansari</dc:creator>
      <pubDate>Sun, 03 Mar 2024 14:00:00 +0000</pubDate>
      <link>https://dev.to/samdaqueen/wheels-and-wires-building-my-arduino-self-driving-car-3f37</link>
      <guid>https://dev.to/samdaqueen/wheels-and-wires-building-my-arduino-self-driving-car-3f37</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Welcome to the "Wheels &amp;amp; Wires" series, where I'll document my journey of building an Arduino-based self-driving car. Join me as I dive into embedded programming and real-time systems, learning as I build, troubleshoot, and discover. I will also try to make this series like a guide, providing detailed insights, code, and solutions to the challenges encountered along the way.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Project Goals
&lt;/h3&gt;

&lt;p&gt;The goal of this project is to construct a self-driving car using an Arduino, actuators, sensors, and a path-finding algorithm. There will be a camera to track the position of the car relative to a goal and detect obstacles in its path. Using data from the camera and onboard sensors, the Arduino will receive wireless instructions to control its movement.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  What to Expect
&lt;/h3&gt;

&lt;p&gt;In each post, I'll provide insights into the challenges I face, the solutions I come up with, and the progress I make. From coding hurdles to hardware integrations, I'll cover it all.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  What's Next
&lt;/h3&gt;

&lt;p&gt;Stay tuned for the next installment, where we'll dive into the details of setting up the project environment and getting started with the hardware.&lt;/p&gt;




&lt;h4&gt;
  
  
  About Me
&lt;/h4&gt;


&lt;div class="ltag__user ltag__user__id__1308995"&gt;
    &lt;a href="/samdaqueen" class="ltag__user__link profile-image-link"&gt;
      &lt;div class="ltag__user__pic"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eLPyww8Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://media.dev.to/cdn-cgi/image/width%3D150%2Cheight%3D150%2Cfit%3Dcover%2Cgravity%3Dauto%2Cformat%3Dauto/https%253A%252F%252Fdev-to-uploads.s3.amazonaws.com%252Fuploads%252Fuser%252Fprofile_image%252F1308995%252Fd46bece3-1746-4731-a144-88fd0686aef3.jpeg" alt="samdaqueen image"&gt;
      &lt;/div&gt;
    &lt;/a&gt;
  &lt;div class="ltag__user__content"&gt;
    &lt;h2&gt;
&lt;a class="ltag__user__link" href="/samdaqueen"&gt;Samreen Ansari&lt;/a&gt;Follow
&lt;/h2&gt;
    &lt;div class="ltag__user__summary"&gt;
      &lt;a class="ltag__user__link" href="/samdaqueen"&gt;Senior Software Engineer @ CrowdDoing | ex-SDE @ Amazon | MS in Computer Science @ NU, Boston&lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
 I'm a Software Engineer by profession and tinkerer by passion. I love everything AI, Robotics, and embedded systems. While I may be new to embedded programming, I am super excited about this project and can't wait to watch my car go zooooom! 🚗🔌🤖

&lt;p&gt;Check out the code on my &lt;a href="https://github.com/SamDaQueen"&gt;GitHub&lt;/a&gt;.&lt;br&gt;
Connect with me on &lt;a href="https://www.linkedin.com/in/samreen-ansari"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Happy tinkering!&lt;/p&gt;

</description>
      <category>arduino</category>
      <category>tutorial</category>
      <category>todayilearned</category>
      <category>ai</category>
    </item>
  </channel>
</rss>
