Trybotics Logo

Arduino Serial Monitor in Tinkercad

DESCRIPTION

Keeping track of everything going on in your program can be an uphill battle. The serial monitor is a way to listen to what's going on in your code by reporting back to the computer over the USB cable. In the Tinkercad Circuits simulator, the Serial Monitor can be found at the bottom of the Code panel and can also be used to graph variables as they change. Use the Serial Monitor to "talk" to the computer as a way to check if the Arduino code is doing what you intended. This can be very useful for solving problems with your code (called ‘serial debugging’).

The two circuits in this lesson use the same configurations as the previous two lessons on digital input with a pushbutton and analog input with a potentiometer. The only differences are that these circuits are free-wired (no breadboard) and do not have an external LED wired up. Oh, and they are running code to generate serial messages, which we'll learn about in this lesson.

You can follow along virtually using Tinkercad Circuits. You can even view this lesson from within Tinkercad (free login required)! Explore the sample circuit and build your own right next to it. Tinkercad Circuits is a free browser-based program that lets you build and simulate circuits. It's perfect for learning, teaching, and prototyping.

Description:

Let's use the code blocks editor to listen to an Arduino input pin, then print out the value (analog) or state (digital) of the input in the Serial Monitor window. Click the "Code" button to open the code panel.

Click on the Serial Monitor at the bottom of the code panel.

Click "Start Simulation" to run the sample Arduino code, and observe the numbers in the Serial Monitor as you interact with the potentiometer. You can click back and forth between the two Arduinos while the simulation is running in Tinkercad Circuits, but only the analog circuit will display in the embedded module above.

After duplicating the sample circuit into your Tinkercad account, you can change the code. Navigate to the Output code category, then drag out a "print to serial monitor" block and place it just before the serial block that's already in the program.

Change the default text to label your Serial data, such as "sensor: " (don't forget the space after the colon), and adjust the dropdown menu to print without a new line.

Start the simulation and observe the change in the Serial Monitor. You can stack serial blocks like this to create useful feedback messages while developing any project code.

Description:

When the code editor is open in Tinkercad Circuits, you can click the dropdown menu on the left and select "Blocks + Text" to reveal the Arduino code generated by the code blocks (not available in the embedded module in the first step). This code sends data from the Arduino to the Serial Monitor, but in a later lesson you can also learn how to receive data from the Serial monitor and two way serial communication.

/*
  DigitalReadSerial

  Reads a digital input on pin 2, prints the
  result to the serial monitor

  This example code is in the public domain.
*/

Before the setup(), we can see a multi-line comment that starts with /* and ends with */.

int buttonState = 0;

At the very start of our program, we'll create a variable to hold the state of the input.

void setup()
{
  pinMode(2, INPUT);
  Serial.begin(9600);
}

Inside the setup, just like in the analog and digital input lessons, the pin connected to the switch or sensor is configured to be an input using the pinMode() function. To be able to send messages, the Arduino needs to open a new communication channel with Serial.begin(). It's like pressing the call button on a phone—from that moment you open a communication line for the call. The argument tells the Arduino how fast to communicate, for instance 9600 bits per second (aka baud).

void loop()
{
  // read the input pin
  buttonState = digitalRead(2);
  // print out the state of the button
  Serial.print("sensor: ");
  Serial.println(buttonState);
  delay(10); // Delay a little bit to improve simulation performance
}

The code inside the loop reads the state of the input with digitalRead() and stores it in the buttonState variable. Then a function called Serial.println() sends the data to the monitor (over the USB cable in the case of a physical Arduino board). If you made the blocks changes to the program in the previous step, you will also have a Serial.print() line of code. println sends a new line after the message, and print does not. Use quotes around text labels, for instance Serial.print("sensor: ");. If you want to make a single line of serial debugging output, you may use several Serial.print() commands followed by a single Serial.println().

Description:

There is a special feature in Tinkercad Circuits called the Debugger. It steps through your code and allows you to peer in at your variables and more.

    With the code editor open, find the Debugger by clicking the button with the bug icon.

    In Blocks + Text mode (or Text-only mode, if you prefer), click a line number to add a breakpoint, where the debugger will stop each time through the loop.

    Start the simulation.

    Hover over variables while paused to see their values.

    Description:

    These circuits are available as circuit starters. You can use these circuit starters anytime you want to read a digital or analog input and print its state to the Serial Monitor.

      Grab Arduino circuit starters from the components panel (dropdown menu -> Starters -> Arduino).

      Description:

      Tinkercad Circuits also has built-in graphing of your serial data, provided the stream doesn't have any text in it. This is handy for visualizing changes in sensor readings and other inputs, as well as for tracking variables in your program.

        With the Serial monitor open, click the graph button to open the graph panel. Remove the sensor label block that you added earlier, or use a fresh Arduino serial starter to create a serial data stream with no text.

        Start the simulation and interact with the input to watch the graph values change.

        Description:

        You have the option to build a physical circuit to go along with this or the digital input or analog input lessons, then use your computer's Arduino software to view the serial data coming in over the USB cable. To program your physical Arduino Uno, you'll need to install the free software (or plugin for the web editor), then open it up.

          Wire up the Arduino Uno circuit by plugging in components and wires to match the connections shown here in Tinkercad Circuits. For a more in-depth walk-through on working with your physical Arduino Uno board, check out the free Instructables Arduino class (a similar circuit is described in the third lesson).

          Copy the code from the Tinkercad Circuits code window and paste it into an empty sketch in your Arduino software, or click the download button (downward facing arrow) and open the resulting file using Arduino.You can also find these examples in the Arduino software by navigating to File -> Examples -> 03.Analog -> AnalogInOutSerial or File -> Examples -> 02.Digital -> DigitalInputPullup.

          Plug in your USB cable and select your board and port in the software’s Tools menu.

          Upload the code to your board, then click the magnifying glass icon in the upper right corner to open the serial monitor. Double check that the baud rate matches the one in your setup Serial.begin(9600).

          Press the pushbutton or turn the knob and watch the numbers change in your Serial Monitor window.

          Description:

          Now that you’ve learned to print to the Serial Monitor, you're ready to test out new kinds of digital and analog sensors, and also learn to read incoming serial data (user keyboard input).

          Can you compose a single circuit and program that prints out both the analog and digital inputs shown in this lesson?

          Here's a link to the pictured circuit, and its Arduino code:

          int int buttonState = 0;
          int sensorValue = 0;
          void setup()
          {
            pinMode(2, INPUT);
            pinMode(A0, INPUT);
            Serial.begin(9600);
          }
          void loop()
          {
            // read the input pin
            buttonState = digitalRead(2);
            // read the input on analog pin 0:
            sensorValue = analogRead(A0);
            // print values to the serial monitor
            Serial.print(buttonState);
            Serial.print(", ");
            Serial.println(sensorValue);
            delay(10); // Delay a little bit to improve simulation performance
          }
          

          Continue on to try a new sensor and combine inputs and outputs, for instance in the temperature sensor LED bar graph lesson, PIR motion sensor lesson, or photoresistor lesson. (coming soon).Use your computer's keyboard to send serial data to your Arduino and interpret it with Serial.read() (lesson coming soon).

          You can also learn more electronics skills with the free Instructables classes on Arduino, Basic Electronics, LEDs & Lighting, 3D Printing, and more.


          YOU MIGHT ALSO LIKE