Arduino is an open-source electronics platform based on easy-to-use hardware and software. Arduino boards are able to read inputs - light on a sensor, a finger on a button, or a Twitter message - and turn it into an output - activating a motor, turning on an LED, publishing something online. - https://www.arduino.cc/en/guide/introduction
It is easy to get started with these 5-minute tutorials from Robotshop.
In Arduino programming, digital and analog inputs and outputs refer to the ways in which the Arduino interacts with the external world, such as sensors, actuators, and other devices.
If you understand these, you should be able to interface with all components. The focus is to understand how digital and analog input and output works.
Think of analog like a smooth, continuous stream, and digital like stepping stones. Analog signals, such as music on a vinyl record or the volume knob on a radio, can be any value within a range. It's like a smooth volume control that can be set to any level.
On the other hand, digital signals, like the numbers on your phone screen, are more like steps – they can only be certain values, like 1 or 0. Imagine turning up the volume on your phone – it goes up in steps, not smoothly. Analog is like a smooth slide, and digital is like climbing stairs.
Understanding digital inputs and outputs lays the foundation for numerous practical applications. From creating interactive projects with buttons and switches to controlling digital devices like LEDs or relays, these concepts are integral to Arduino programming.
In the upcoming tutorials, we'll explore more advanced topics, combining digital inputs and outputs to build interactive and responsive Arduino projects.
Digital inputs are the eyes and ears of your Arduino. They are sensors that detect binary signals – either HIGH (5V) or LOW (0V). These inputs are essential for capturing information from buttons, switches, or any device providing a simple on-off response.
Digital outputs, on the other hand, are the hands and voice of your Arduino. They can send binary signals to control external devices, typically turning them on or off. Digital outputs are crucial for tasks like lighting LEDs, triggering relays, or driving digital actuators.
Basic introduction to digital inputs and outputs
Task 1: Flashing LED
Follow the tutorial above enough to create your first flashing LED. Please follow the sequence so you learn about conventions as well and how to use the multimeter.
Task 2: Read the state of a button switch
Follow the tutorial above to create a button switch input.
Task 3: Knight Rider sequence
Add 5 LEDS to the canvas and create a Knight Rider sequence. The start of the design has been done for you. Use arrays for your LEDs as shown in the code.
Don’t forget to name the file, add a docstring and comments as you go.
Make sure you use appropriate colour code: Red is positive and black is negative or ground. Choose other colours for the rest.
There are two important concepts when using the Arduino with the analog world. Because the Arduino is a digital device it is about high and low only (two states, 5V or 0V). To work with Analog interfaces we need to find ways to modify the circuit or program to do that for us. It is important for the Arduino to interact with analog devices as many inputs (for example sensors ) and outputs (for example motors) needs analog signal to do what we want them to do. These two concepts are pulse width modulation (PWM) and analog to digital converter (ADC).
Microcontrollers, the brains of many electronic devices, speak the language of digital. They understand 1s and 0s. But the real world often speaks in analog – temperatures, light levels, and sounds are continuous.
Analog Output - PWM
Analog output in Arduino is like making a dimmer switch for your lights. Imagine if you could only turn the lights on or off - that's like digital. But with analog output, it's like having a magical switch that lets you make the lights brighter or dimmer gradually.
Arduino achieves this by using a trick called Pulse Width Modulation (PWM). Instead of just turning the lights on or off, it quickly switches them on and off really fast. Unlike true analog signals, which can have an infinite number of values within a range, PWM is a way to simulate an analog output using digital signals.
So how does it work?
By adjusting how long the lights are on compared to off, you can control how bright they appear, kind of like magic! Because of the speed the output is changing from 5V to 0V, the output will “look” like one voltage. It is all about average voltage.
In robotics, PWM is widely used for controlling the movement of servo motors and for driving DC motors with variable speeds. This allows for precise control of robot limbs, wheels, or other moving parts.
PWM is commonly employed for adjusting the brightness of LEDs. By varying the duty cycle, you can make the LED appear dimmer or brighter without changing the overall power consumption.
The PWM output pins are 8-bit pins. This means that the range in decimal is 0 to 255( 2^8).
Task 4: LED Brightness Control
Connect an LED to a PWM-enabled pin on your Arduino(The pins with ~ in the front). Add a multimeter so we can measure the output voltage.
Write a program to gradually increase and decrease the LED brightness using PWM.
Add Docstring
Variable Declarations
As this is digital output, pinMode must be declared "pinMode(pin, OUTPUT)"
Use analogWrite to write a PWM value (0 to 255)
Observe how changing the duty cycle affects the perceived brightness.
What is special about the relationship between the voltage and the binary number output? For this create a table with the PWM value and output voltage. Measure voltage with a multimeter for the various PWM output values.
Add a FOR loop so that the LED goes brighter and fade continuously.
Task 5: Servo Motor Positioning
Connect a servo motor to a PWM-enabled pin on your Arduino.
Write a program to control the servo motor's position by varying the PWM signal. Don't use a library.
Understand how different duty cycles correspond to different angles of rotation.
You can then try the servo library.
Task 6: RGB LED Colour Mixing
Use an RGB LED and connect its Red, Green, and Blue pins to separate PWM-enabled pins on your Arduino.
Write a program to create various colours by adjusting the PWM values for each colour component.
Explore the combination of different duty cycles to produce different hues.
Connect a piezo buzzer to a PWM-enabled pin on your Arduino.
Write a program to generate different tones by adjusting the PWM frequency and duration. You can use the tone() and noTone() functions.
Explore how changes in duty cycle affect the pitch of the sound.
Difficult to get started? Work through this tutorial
Connect multiple LEDs to different PWM-enabled pins on your Arduino.
Write a program to create fading patterns on these LEDs, experimenting with different PWM values for each LED to create dynamic lighting effects.
ADC
As we have said before, microcontrollers are digital devices and many times we need to communicate with the outside world. That's where ADCs (Analog-to-Digital Converters) come in. They're like translators, turning the smooth, continuous signals from the real world into the digital language that microcontrollers understand. This way, your microcontroller can read data from sensors, like a temperature sensor, and make decisions based on that information, bringing the digital world and the analog world together.
So how do we do it?
Sampling: The ADC takes "snapshots" of the continuous analog signal at regular intervals, like taking pictures every second of your friend turning the volume knob.
Quantization: It then assigns a number to each snapshot based on the signal's height at that moment. Think of it like rounding the volume level to the nearest whole number.
Conversion: The ADC turns each of these rounded numbers into a binary code, a series of 1s and 0s, which is the language the microcontroller speaks.
Digital Output: Now, the microcontroller has a series of these binary numbers that represent the original analog signal. It can use these numbers to make decisions, like increasing the volume when the number goes up.
So, in simple terms, the ADC converts a smooth, continuous analog signal (like turning the volume knob) into a digital language (a series of 1s and 0s) that a microcontroller can understand and work with.
This video explains it well but way more than a students need to understand to make it work.
Task 9: Voltage Measurements
Watch this video, create the code and follow the instructions to test your voltages.
In this code you will learn about the "map" function. Very handy to know.
You can do this with Tinkercad Circuits or the Arduino App.
What actually happens
The input to the ADC is 0 to 5V for the Arduino or most microcontrollers. This voltage is converted into a binary number the microcontroller can understand.
Task 10: Reading a Potentiometer
To see the effect we can use a potentiometer on the input to the Arduino analog port( A0 to A5). We can’t just use a resistor, we have to convert the change in resistance when the pot is turned to a voltage. For this we need a voltage divider circuit.
In this case you can use a multimeter to measure the voltage when the pot is turned. Simulations are handy with this or build this circuit with real components, a breadboard and a power supply or battery.
The next step is to build this on a breadboard. For this exercise we will use Tinkercad Circuits. What is important is never short the 5V and ground so the middle pin is the wiper. Make sure that the voltage increases when the potentiometer is turned clockwise.
Reading the Analog Value
Connect the pot’s wiper to the analog input of the Arduino Uno. In this case we are using A5 but you can use any of the ANALOG IN pins.
Create the code step by step:
Add Docstring
Variable Declarations
As this is Analog Input, pinMode not important
Start Serial communications (Serial.begin)
Use analogRead to read value from A5 and display on serial monitor.
Complete the following table turning the potentiometer.
Use an LDR, in a voltage divider circuit configuration, to determine how bright it is.
Using a temperature sensor, create a thermometer that displays the current temperature in the serial monitor.
Challenge: Display temperature on a LCD display.
Difficult to get started? Work through this tutorial
Teacher Instructions
Decomposition is a real stumbling block for students. These videos explain a little bit about decomposition, what it is and how you can go about it. A sound knowledge of the "Building Blocks" is really important. ie you can't decompose a problem if you don't know what "bits" it is mad up from. So for decomposing Arduino problems, the students have to have a good solid knowledge of Arduino C.
Once the students have watched the videos, they will have to try to do a task to decompose something they are more familiar with (where they know the "bits" well) - getting a glass of water!
The discussion is more important that the answer. Can they identify the "sequence" (the order of steps) , "selection" (the if this...do that...else...), "iteration" (the loop until, repeat until) and "storage" (what do they need to remember or refer to) ?
Next is for students to understanding subsystems: Input, output, processes and feedback.
Decomposition
It involves breaking down a complex problem or system into smaller parts that are more manageable and easier to understand. The smaller parts can then be examined and solved, or designed individually, as they are simpler to work with.
Flowcharts
Using a flowchart is an easy way to show how a problem can be broken down into simple single steps.
Subsystem design is closely linked with the concept of decomposition in system design. Decomposition involves breaking down a complex system into smaller, more manageable parts or components. Subsystems, in the context of Arduino or any other embedded system development, are the result of this decomposition process.
In the context of developing Arduino-based projects, subsystems are essential for several reasons:
Modularity and Scalability: Breaking down a project into subsystems allows you to design and develop smaller, more manageable components. Each subsystem can be treated as a module with specific functionalities, making it easier to understand, test, and debug. This modularity also facilitates scalability, as you can add or modify subsystems without affecting the entire system.
Code Organization: Subsystems help in organizing your code in a more structured manner. This makes the codebase more readable, maintainable, and easier to update or troubleshoot. With well-defined subsystems, it becomes simpler to locate and fix issues within a particular part of the code.
Collaborative Development: When working on projects with multiple team members, breaking the system into subsystems allows different individuals to focus on specific components. This promotes parallel development, as team members can work on different parts of the project simultaneously, provided there is clear communication and coordination.
Debugging and Testing: Isolating subsystems makes it easier to identify and rectify errors. By testing each subsystem independently, you can ensure that individual components function correctly before integrating them into the complete system. This approach simplifies the debugging process and reduces the complexity of identifying issues.
Reuse of Code: Well-designed subsystems are reusable in other projects. Once you have a subsystem that performs a specific function reliably, you can use it in future projects without having to rewrite the entire code. This not only saves time but also promotes the creation of a library of modular, reusable components.
Resource Management: Arduino boards often have limited resources, such as memory and processing power. Subsystems help manage these resources more efficiently by allowing you to activate and deactivate specific functionalities as needed. This can be crucial for optimizing the performance of your Arduino-based project.
Encapsulation of Functionality: Each subsystem encapsulates a specific set of functionalities. This encapsulation not only simplifies the development process but also enhances the overall robustness of the system. Changes or updates to one subsystem are less likely to impact others if proper interfaces and dependencies are managed effectively.
In summary, subsystems play a crucial role in the development of Arduino projects by promoting modularity, organization, collaboration, ease of testing, code reuse, resource management, and encapsulation of functionality. These principles contribute to more efficient, maintainable, and scalable Arduino-based systems.
When working on your circuit, focus on three main steps: design, create, and test each part separately. Keep a record of your circuit and code for each part. If something doesn't work as expected, you can easily go back to where you started. If your project has different parts, create separate functions for each. This makes it easier to understand and fix any issues that may come up.
This video explains how subsystems are used on a very basic circuit. Using an LDR, the LED must turn on when it is dark. For this we want to use three subsystems:
Input subsystem: This consist of the LDR in a voltage divider circuit. Light is the input
Output subsystem: LED and current limiting resistor
Processing subsystem: This subsystem is where we convert the input to the output. The amount of light determines if the LED is on or off.
Note: The Resistance range with an actual LDR will be different than the simulation.
Task 14: Flashing LED on Button Press
Using the techniques above, create a circuit and code that flashes an LED when a button is pressed.
Teacher Notes
The easiest is to use Tinkercad Circuits to works through these tutorial. The purpose is to end up with a good understanding of digital and analog inputs and output applications.
This video series will cover the important things you have to keep in mind when you write code for humans.
Code for Humans means code that is meant to be read by other humans (code comments, docstrings, good variable names etc) and code that is meant to be used by humans (error checking, good error messages, good instructions, crash proofing etc.)
You'll also walk through a simple example of a problem, similar to your assessment problems. In that, you will learn to write highly readable and unbreakable code that neatly handles input errors.
Students: Write the "Can I Vote" program together with the instructor and see if you can work out how and why it works.
Arduino's Plug and Make Kit Offers an Easy Introduction to the Internet of Things
The 25 Best Jetson Nano Projects in 2024. The Jetson Nano is a great way to get started with AI. Check out these clever Jetson Nano projects that make the most of its capabilities!
The ever-growing Arduino community is made up of everyone from hobbyists and students to designers and engineers all across the world.
The leading magazine for 3D printing, with compelling content on additive manufacturing, 3D scanning, CAD, laser cutting & engraving, CNC, SBCs, and more.
Best websites to learn more advanced Arduino applications
SparkFun is an online retail store that sells the bits and pieces to make your electronics projects possible. With lots of tutorials as well.