Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: CSC1300_EduBaseV2_Lab0 mbed_blinky EduBaseV2_Lab0 mbed_blinky ... more
Lab 1 Inputs and Outputs
Lab 1: Introducing Digital Input/Output (I/O)¶
Objective¶
In this lab, you will learn the basics of digital input/output (I/O) by programming the pushbutton switches and LEDs.
Bits and Binary¶
Computers store and process lots of data. This data can represent any piece of information from colors to geographic coordinates to college grades. Data in computers is stored digitally, meaning that all data is represented by discrete values that can be counted like 0, 1, 2, 3, etc.. Thus, a digital system is a computerized system that stores and processes data in a digital, discrete form. (You will learn more about the difference between digital vs. analog in the next lab.)
All computer data is in binary, which means that each unit of information, known as a bit (short for "binary digit"), has only two states: 0 or 1. A light switch can be represented as a bit. When the switch is turned off, the switch's/bit's value is 0. When the switch is turned on, the switch's/bit's value is 1. In C++, the bool data type works in the same way, which is why TRUE can be represented as a 1 and FALSE can be represented as a 0.
Because bits can mean anything, it is up to the engineer of a digital system what these bits represent. For example, let's look at the following ordered group of bits 01001100, which is a byte, a group of 8 bits. 01001100 could represent the base-10 number 76, which LEDs on a wire are turned on or off, a combination of pressed switches, a password... the possibilities are endless.
What is Input/Output?¶
A computer must get its data from somewhere and then send out what it does with that data elsewhere. Input/Output, or I/O, is how a computer or digital system communicates and interacts with the outside world.
Inputs are how a computer obtains the data it uses to complete a task. The pushbutton switches on the EduBase can be read as digital inputs with each switch having only two possible states, pressed or not pressed. If you are reading sw5, pressing down sw5 will send a 1 (a TRUE state) to the microprocessor on your Nucleo board, and not pressing sw5 will send your Nucleo board a 0 (a FALSE state). You can create a program that tells your EduBase to perform certain tasks based on the current values of your inputs.
Outputs are how a computer communicates the data it processes to the outside world. Outputs are essentially what the computer produces after it has processed its input data. The LEDs on the EduBase are digital outputs that you can program on or off depending on the value of your inputs. If you set led0 to a TRUE state (sending it a 1), it will light up. If you set led0 to a FALSE state (sending it a 0), it will turn off.
In short, on the EduBase...
Switches are digital inputs:
Switch is pressed = TRUE = 1
Switch is not pressed = FALSE = 0
LEDs are digital outputs:
LED is on = TRUE = 1
LED is off = FALSE = 0
Reading Switches and Writing to LEDs¶
You might be asking yourself, how can I read the switches and write to the LEDs on the EduBase?
To read a switch (your input), you can assign the value of the switch to a variable like so:
bool sw5_pressed = sw5;
This line creates a bool variable called sw5_pressed, which represents whether sw5 is pressed or not. sw5_pressed will evaluate to FALSE if sw5 is not pressed (sending a 0 to the microprocessor) and to TRUE if sw5 is pressed (sending a 1 to the microprocessor).
To write to an LED (your output), you can assign it a bool value to control whether it is on or off. For example,
led0 = TRUE;
will tell led0 to turn on. On the other hand,
led0 = FALSE;
will tell led0 to turn off.
You can also assign a variable to a digital output as well.
bool sw5_pressed = sw5; led0 = sw5_pressed;
The above two lines stores the current state of sw5 into a bool variable called sw5_pressed. The value of sw5_pressed determines whether led0 is on (sw5_pressed is FALSE) or off (sw5_pressed is TRUE).
You can consolidate the previous two lines of code into one line. Figuring this out is this week's lab assignment.
Important Note about I/O on the EduBase
Because the Nucleo F031K6 board has a limited number of I/O pins, which are used to read from and write to the peripherals on the EduBase, not all of the components on the board can be programmed. You have the ability to read only two of the pushbutton switches, sw4 and sw5. You also can write to only three of the LEDs: led0, led1, and led2. (led3 will light up when you press sw4 since their pins are connected on the Nucleo. For hardware reasons beyond this course, you cannot program led3 directly.)
Your Assignment¶
Taking everything that you have just learned, create a program does the following with only TWO lines of code:
1. When sw5 is pressed, it turns led0 on.
2. When sw5 is NOT pressed, it turns led1 on.
Don't forget to save, commit, and publish your program once you are done. Submit the URL to your unlisted program in the comment box in this week's lab assignment's submission page in iLearn with your ZIP file containing your solutions from the first half of the lab.
Help
Here are a few tips to help you get started.
1. Mbed code, like C and C++, is case-sensitive. The input for this lab is written as sw5, and the outputs for this lab are written as led0 and led1. If you accidentally capitalize the digital I/O names like LED0 or Sw5, your program will throw a compiler error.
2. Make sure that each line ends in a semicolon (;).
3. Write your code within the inner loop of braces ({...}) under the comment that says INSERT SOLUTION HERE. Do not touch any code outside of these braces, or your program may not compile or run correctly.
4. You will only have to use two operators in this lab. You have already been shown how to use the assignment operator (=) for outputs and variables. If you want to invert a value, which logical operator do you use?
5. If you are stumped, ask yourself, do you really need to create a variable?