Electronics and Arduino
What is the Arduino?
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.
Learn the fundamentals of Arduino C
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 Interfaces
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.
Understanding Analog Interfaces
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).
Why it matters for microcontrollers
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.
Difficult to get started? Work through this tutorial
Task 7: Sound Generation with a Buzzer
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.
Task 8: LED Fading Patterns
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.