Introduction: Programming the Arduino (Simulator)

Picture of Programming the Arduino (Simulator)

The following information is a single lesson in a larger project. Find more great projects here.

Return to Previous Lesson: Parts in the Kit

Lesson Overview:

Now we'll program a simulated Arduino!

Step 1: Introduction

Before you start controlling the world around you, let’s take a few minutes to navigate the circuit simulator.

We’ve already gone over the anatomy of the Arduino board, but in order to use it you need to program the microcontroller. You do this using the “Arduino IDE” (IDE = Integrated Development Environment) software, which allows you to write programs, which we call "sketches," and upload them.

This function is built into the simulator.

If you are controlling a physical Arduino board, you will need to download the Arduino IDE to your computer -- instructions for doing this will appear in the next lesson

  1. Continue to the next step.

Step 2: Communicating With the Arduino

Picture of Communicating With the Arduino

Click on the “Code Editor” button to open up the IDE interface, which allows you to send instructions to the Arduino.

When you open the Code Editor for the first time, you will see that there is already a program written in the IDE.

If part of the text is not visible, you can expand the Code Editor window by hovering over the top edge to reveal an arrow and clicking and dragging it up.

  1. Click on the "Code Editor" button to reveal the IDE interface.
  2. Continue to the next step.

Step 3: Pre-loaded Program

Picture of Pre-loaded Program

In this step, you will visualize what this program does using the simulated Arduino Board in the Workplane

  1. To visualize what this program does, click the “Start simulation” button (shown above).
  2. Tip: if the code editor is blocking your view of the Arduino Uno, you can minimize it by clicking the “Code Editor” button again.
  3. The first thing to notice is that the red power light on the Arduino turns on. Within the simulator, the board always receives power even though no cables are connected!
  4. You should also notice a blinking light. This program simply blinks the on-board LED (connected to pin 13) on and off at a constant rate.
  5. Continue to the next step.

Step 4: Editing a Program

While the simulator is running, you can actually make changes to the program. In this step you will change the blinking rate of the LED.

Start by opening the Code Editor window again. Find the following text: digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)delay(1000); // wait for a seconddigitalWrite(led, LOW); // turn the LED off by making the voltage LOWdelay(1000); // wait for a second You will learn what all of these commands mean in the Arduino projects, but let's focus on the lines that control blinking rate.

  1. Open the Code Editor window again.
  2. Change the delay time by changing the numbers in parentheses from 1000 to 100. The command will look like delay(100);
  3. Continue to the next step.

Step 5: Uploading and Running a Program

Picture of Uploading and Running a Program

Once you have changed the program, you will need to upload it to the board.

In the simulator, you do this by clicking the "Upload & Run" button in the Code Editor window.

  1. Click the "Upload & Run" button at the top of the Code Editor.
  2. You should see the blue “Stop Simulation” button in the top right corner change to “initializing…” for a moment during the upload and green text reading “successfully compiled” will appear at the top of the Code Editor. You can press "Upload & Run" a few times to make sure you see all of theses messages!
  3. After you change the program, the LED should blink much faster! Instead of having 1000 milliseconds (1 second) between flashes, now there are only 100 milliseconds (1/10th of a second) between them.
  4. Continue to the next step.

Step 6: Detecting a Problem (1)

While you are writing an Arduino sketch, sometimes you will make a mistake and the program can't be compiled (saved) and uploaded. This happens to professional programmers all the time!

The simulator can tell you when a problem occurs and how to fix it. To try this out, let's put the following mistake in the code:

Change the second to last line (line 16):

delay(100);

to:

delay(100)

by removing the semicolon at the end.

Afterwards, try compiling and uploading the code by clicking the "Upload & Run" button.

  1. Remove a semicolon (;) from line 16 in the code.
  2. Press the "Upload & Run" button to try loading the new code.
  3. Continue to the next step.

Step 7: Detecting a Problem (2)

Picture of Detecting a Problem (2)

When you try to upload the code, it won't work!

You will see the text "Sorry it seems like your code has some errors..." in a bright red stripe below the Code Editor.

  1. Replace the semicolon (;) after the delay(100) command.
  2. Try running the code again by clicking the "Upload & Run" button.
  3. Check out the text below the red stripe. This tells you exactly where the problem is! In this case, it tells you that a semicolon is expected before line 17. Notice that line 16 is highlighted in gray -- the semicolon is missing from the end of this line of code.
  4. This information helps you to troubleshoot your code. Next, try putting the semicolon back and upload and run the code again. It should work this time, and the LED on the Arduino will start blinking again!
  5. Continue to the next step.

Step 8: Review

Congratulations, you are now in control of your virtual Arduino board! You have seen a lot of things that the Code Editor can do within the circuit simulator, including uploading a program to the Arduino and detecting mistakes in the code. You will have plenty of practice using the editor in the Basic Kit projects.

In the next lesson, you will learn how to install the Arduino IDE on your computer.

Next Lesson:Programming the Arduino (Kit)

Comments

About This Instructable

1,328views

1favorite

License:

Bio: Circuit lessons for Tinkercad Circuits!
More by circuits:IntroductionProject 15: Hacking ButtonsProject 14: Tweak the Arduino Logo
Add instructable to: