Setting Up Environment and Components📟
Tags: arduino, tutorial, todayilearned, ai
Published: 2024-03-03 14:00:00

## Introduction

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.

## Table of Contents
&nbsp;1. [Introduction](#introduction)
&nbsp;2. [Table of Contents](#table-of-contents)
&nbsp;3. [About Arduino Sketches](#about-arduino-sketches)
&nbsp;4. [The Components](#the-components)
&nbsp;5. [Making the LED on the Arduino blink](#making-the-led-on-the-arduino-blink)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 5.1. [IDE Setup](#ide-setup)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 5.2. [Connecting the Arduino to VSC](#connecting-the-arduino-to-vsc)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 5.3. [Writing our first Arduino Sketch!](#writing-our-first-arduino-sketch)
&nbsp;6. [What's Next](#whats-next)
&nbsp;7. [Resources](#resources)

## About Arduino Sketches

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 _C_ and _C++_. These sketches are uploaded to the Arduino microcontrollers. They consist of two main parts: `setup()` and `loop()`.

> The `setup()` 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.

> The `loop()` function is executed repeatedly after the `setup()` 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.

Now that we have a basic understanding of Arduino sketches, let's move on to setting up our development environment.

## The Components
1. Arduino MEGA2560 board
2. USB Cable Type-A/C to Type-B 
![Arduino and Cable](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9hsadabw78islwrplfgj.jpg)

## Making the LED on the Arduino blink

Now let's set up the IDE, connect to Arduino, and write code.

### IDE Setup

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

1. **Download and install the IDE**
We can use this [link](https://code.visualstudio.com/download) to download the IDE and then go through the installation steps

2. **Install extensions**
Go to the [Extensions Marketplace](https://code.visualstudio.com/docs/editor/extension-marketplace#:~:text=You%20can%20browse%20and%20install,on%20the%20VS%20Code%20Marketplace.)`(Ctrl+Shift+X)` in VSC and install these: 

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

### Connecting the Arduino to VSC

1. Plug in the Arduino to the PC using the cable
2. Open Device Manager and look for the connected COM port
![Device Manager](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bjk0rv0vzt7rn5hhaxhu.png)
3. Go to VSC and click on each of these board settings at the bottom of the window
![Board settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5gljkroqb94whn3km0hj.png)
 1. **Select the Correct Board**: 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.
![Board Manager](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dtzrhw2wrbu5i64jv8hq.png)
 2. **Choose the Serial Port**: 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.
 3. If there are multiple sketch files in the directories, make sure the correct one is selected. After the selections, it should look like this:
![VSC Board Settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hkb4cgj8z4txipm5cooj.png)

### Writing our first Arduino Sketch!
Now that we have the IDE and components ready, we can write a basic sketch to make the LED on the Arduino board blink.

1.&nbsp;Open VSC to any preferred working directory 
2.&nbsp;Create a directory named `blink` in the working directory
3.&nbsp;Create a sketch file `blink.ino` inside the `blink` directory

> Make sure to name the sketch file the same as the directory containing it!

4.&nbsp;We write the code in our `blink.ino` file
```cpp
const int LED_PIN = LED_BUILTIN; // the output pin number

void setup() {
  pinMode(LED_PIN, OUTPUT); // set the pin as output
}

void loop() {
  digitalWrite(LED_PIN, HIGH); // turn the LED on
  delay(1000);                 // wait for a second
  digitalWrite(LED_PIN, LOW);  // turn the LED off
  delay(1000);                 // wait for a second
}
```
5.&nbsp;Code explanation
```cpp
const int LED_PIN = LED_BUILTIN; // the output pin number
```
> 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 `LED_BUILTIN`. We can use external LEDs connected through wires but must be sure to use it with a resistor to avoid burning up the LED.

```cpp
void setup() {
  pinMode(LED_PIN, OUTPUT); // set the pin as output
}
```
> The `setup()` function will run once at the program's start. Here we specify that the LED_PIN is an output pin. `pinMode()` sets the given pin component to INPUT, OUTPUT, or INPUT_PULLUP (HIGH if nothing is connected).

```cpp
void loop() {
  digitalWrite(LED_PIN, HIGH); // turn the LED on
  delay(1000);                 // wait for a second
  digitalWrite(LED_PIN, LOW);  // turn the LED off
  delay(1000);                 // wait for a second
}
```
> The code within the `loop()` 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 `digitalWrite()` to set the value of the pin as `HIGH` or `LOW`. `delay()` adds the specified millisecond pause to the program.

6.&nbsp;Run the code
![Upload and Verify](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bq9p9afh0xx5cbuxomur.jpg)
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).
 - Click on Verify to compile the code and check for errors
 - Click on Upload to send the code to the connected Arduino
 - Watch the LED blink! Yayyy! 🎉

7.&nbsp;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!

![Blinking LED](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xribukhqg28tnx8klogr.gif)

&nbsp;
There we have it! Our Arduino sketch serves as the first step towards building that car!

&nbsp;
### What's Next

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

***
### About Me
{% user samdaqueen %} 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! 🚗🔌🤖

Check out the code on my [GitHub](https://github.com/SamDaQueen).
Connect with me on [LinkedIn](https://www.linkedin.com/in/samreen-ansari).

Happy tinkering!
***
#### Resources
- [Arduino Official Website](https://www.arduino.cc/)
- [Arduino Documentation](https://www.arduino.cc/reference/en/)
- [How to Use VS Code for Creating and Uploading Arduino Sketches](https://www.circuitstate.com/tutorials/how-to-use-vs-code-for-creating-and-uploading-arduino-sketches/)