DEV Community

Cover image for Setting Up Environment and Components📟
Samreen Ansari
Samreen Ansari

Posted on • Edited on

Setting Up Environment and Components📟

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

 1. Introduction
 2. Table of Contents
 3. About Arduino Sketches
 4. The Components
 5. Making the LED on the Arduino blink
       5.1. IDE Setup
       5.2. Connecting the Arduino to VSC
       5.3. Writing our first Arduino Sketch!
 6. What's Next
 7. 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

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, which now has debugging support as well. I prefer using Visual Studio Code (VSC) 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 to download the IDE and then go through the installation steps

  2. Install extensions
    Go to the Extensions Marketplace(Ctrl+Shift+X) in VSC and install these:

  • We should install the Arduino extension 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 (optional)
  • We can get better syntax highlighting for the code by using the Better C++ Syntax extension (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
  3. Go to VSC and click on each of these board settings at the bottom of the window Board settings
    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
    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

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. Open VSC to any preferred working directory
2. Create a directory named blink in the working directory
3. 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. We write the code in our blink.ino file

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
}
Enter fullscreen mode Exit fullscreen mode

5. Code explanation

const int LED_PIN = LED_BUILTIN; // the output pin number
Enter fullscreen mode Exit fullscreen mode

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.

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

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).

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
}
Enter fullscreen mode Exit fullscreen mode

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. Run the code
Upload and Verify
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. 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

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

 

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

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.
Connect with me on LinkedIn.

Happy tinkering!


Resources

Top comments (0)