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 thesetup()
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
- Arduino MEGA2560 board
- USB Cable Type-A/C to Type-B
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.
Download and install the IDE
We can use this link to download the IDE and then go through the installation stepsInstall 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
- Plug in the Arduino to the PC using the cable
- Open Device Manager and look for the connected COM port
- Go to VSC and click on each of these board settings at the bottom of the window
- 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.
- 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.
- If there are multiple sketch files in the directories, make sure the correct one is selected. After the selections, it should look like this:
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
}
5. Code explanation
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.
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).
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 usedigitalWrite()
to set the value of the pin asHIGH
orLOW
.delay()
adds the specified millisecond pause to the program.
6. Run the code
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!
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!