Arduino Notes 1
Arduino Notes 1
● Microcontroller programming
● Adreno is a platform and an environment not just a specific product it's a collection of
open-source microcontroller boards which contain a small piece of code called the
arduino bootloader this code allows us to integrate with the arduino IDE which provides
a set of libraries arduino specific libraries designed to replace the more complex
intricacies of microcontroller programming with easy-to-use functions and methods.
● Arduino Parts 2:
https://www.circuitmix.com/what-are-the-essential-electronic-parts-for-arduino-projects/
● Arduino Components:
http://kkfscs.weebly.com/arduino-components.html#:~:text=Resistors%20are%20used%
20to%20reduce,converts%20the%20excess%20to%20heat
Microcontroller
● A microcontroller is like a basic computer it is much simpler and has much less power
than even the most basic PC however it consists of the same component a processor
Ram flash storage and hardware peripherals such as GPIO pins two connecting
sensors and circuitry
● To most Arduino boards which use at mills 18 mega series chips run at 16 megaHertz
while other boards which run on ARM processors can run at several hundred megaHertz
it means that the CPU clock ticks 16 million or greater times per second and on each
clock cycle the CPU can perform a simple instruction it then groups these simple
instructions together to do bigger tasks like displaying data or reading an input.
● Megahertz it's a really big number and it means times per second. Hertz is a measure
of frequency.
○ So every second there is an internal electrical clock on the chip which goes
up and down up and down up and down at a set interval and every time there
is a clock edge when it changes states it is a new cycle and so every time for
example if it goes into a low stage then the microcontroller is able to do
something now this is very very simple instructions we're not talking about
the fact that every time it falls on one of these clock edges it can display data on
an LCD screen the complex code behind that requires a much lower level of
integration microcontrollers are based around mathematics so on each of
these clock cycles it could be as simple as adding two numbers together
binary numbers and zero or comparing the state of one number against the
other by doing this you can group lots of small lower-level functions into tasks
that are much more complex such as what you see on modern computers or
even turning an LED on or off a bullock-cart further on in the chapter
microcontrollers are a blank slate apart from the bootloader they will only
do exactly what you told them to do and nothing else there's no operating
system running nothing else going on except your code
● C and C++ can be used for many things but really excel at controlling hardware devices
such as microcontrollers there is much debate about whether Arduino uses C or C++ as
they are both quite similar especially when you're only using them for basic usage
however whilst Arduino libraries are usually dot cpp files the arduino library is based
upon a software abstraction called wiring.
● Wiring allows for easy control of hardware ports through simple functions without
needing to consult data sheets and get bogged down in pin mapping. The end result is
that the arduino uses bits of both C and C++ but the general flow and structure of the
code is more heavily based around C.
● The easiest way to power your Arduino is via the USB port as it provides power and
communications for uploading programs and using the serial port.
● You can power the Arduino using the 2.1 millimeter sent to positive barrel jack which
accepts 9 to 12 volts DC however you'd also need a USB connection to communicate to
the computer there is also a V in pin on the Arduino you know power header which pros
you to bypass the 5 volt regulator and give it 5 volts directly however this is only
recommended for advanced designs due to the lack of protection in the event of a
voltage spike or incorrect power supply we recommend using the USB port for powering
and programming your Arduino IDE especially lost prototyping as it is the safest and
most reliable method.
● Microcontrollers containing flash memory which is where the code is stored flash
memory is non-volatile memory which means that it retains information up to power has
been disconnected.
● A lot of this is important and that means you don't have to re-upload your code every
time you unplug your board.
● The function pinMode() now we only want this to run once because what it does is it
takes a pin and configure that is either an input or an output and there are cases
where you may want to do this multiple times throughout your program but generally
you want to set that pin as an output and leave it that way so I'm configuring pin 13
which is the pin with the onboard led connected to it and I'm configuring that as an
output using a semicolon to finish.
● Void loop() is a function which runs directly after void setup and will run over and over
and over again until you tell it to stop or you unplug power so loops continuously it's
called an infinite loop.
● The digitalWrite() outputs either a 1 or a 0; 5 volt or 0 volt to that particular pin and
we'll cover the electronics and voltage and current and all the rest in the next chapter
however we can really simply say that when it's higher we're turning the pin on and
when it's low we're turning the pin off because that power LED is connected up to it
so digitalWrite() we're writing to pin 13 and we're writing it high so it’s turning the LED on
then we're going to wait for half a second so that our item perceives that it’s on.
● The delay(500) means to delay in 500 milliseconds or half a second (0.5 second). 1,000
milliseconds is equivalent to 1 second.
● Then we'll turn it off using the same digitalWrite function again targeting pin 13 and
turning it low then we'll put another delay in for 500 milliseconds and then it'll loop and
repeat over and over again which gives the effect of it blinking.
● So now that we have our code ready, we need to tell the computer which device we
would like to upload that code to go to the Tools menu and select the correct board type.
● Then select the corresponding comport or the Mac OS users should be a slash device if
it's not displayed there to ensure that your Arduino is connected by USB to the computer
and that the power light on the Arduino is on.
● Now you can click the upload button located in the top left-hand corner which will first
compile your code and check it for any errors and then upload it to your board
while it's going if everything went smoothly you'll see a success message in white
however error messages will be displayed in orange.
Figure 1.2 - Upload Button
Figure 1.3 - Compiling
Program Structure Overview
● Your program will run from top to bottom in a linear fashion now.
● Setup functions use the first thing your program does when it starts to run
everything inside the setup function highlighted here will run once and only once
and when it reaches the end of the setup function unless it's told otherwise it will move
straight into the void loop function the loop function will repeat over and over and
over and over again until it is told otherwise all the devices turned off power removed.
● The goal of any programmer is to create code that is elegant. A little written piece of
code will be easy for others to understand even if they don't fully get every small detail
they can look at it and understand the intended outcome and process.
● Styling and formatting of your code one of the biggest things you can do to increase
the readability of your code is to use proper indentation so what do we mean by
indentation well a good text editor will help you out a lot here.
● The general rule is that each level or nest of your code to be indented once further
to the right in the previous level for example everything in our code as you can see
here starts at a zero indentation so the function void loop avoid setup to all the way to
the left however when we write code inside the word function it gets a new level so it get
indented once further to the right you can see we press the tag here and it keeps
everything in life another statement or function inside the loop would be indented twice
to the right or once again and so on and so forth.
Figure 1.0 - Indentation Sample
Using Variables
● A variable is used to store information in your code. It has a name, a data type and it
takes up space in your processor's memory or on the chip memory.
● A variable is used to store information in your code. It has a name, a data type and it
takes up space in your processor's memory or on the chip memory.
● Schematic is a fancy word for an electrical circuit in symbols. It's not a physical
layout of how the circuit will look when it's on a printed circuit board but it's designed to
really clearly and easily show what is connected to where and how the circuit
works.
● Using the digital pins of the Arduino you know to read digital inputs and control digital
signals microcontrollers and computers as digital devices they only work with digital
signals binary signals 1 or 0.
● By grouping these ones and zeros together you can create much larger complex data. A
digital pin is exactly the same. It has either on or off states and we call this high or
low when it is high or 1 it outputs 5 volts on a pin when it is low at output 0 volts.
● A button is a good example of a digital input when it is pressed it could output a low
signal to indicate the press and when it's released a five volt signal that's known as
active low logic which is what we're going to use.
● Input Pullup Serial monitors the state of a switch by establishing serial communication
between your Arduino and your computer over USB. Additionally, when the input is
HIGH, the onboard LED attached to pin 13 will turn on; when LOW, the LED will turn off.
● What a Pull-up Resistor does is there's these small resistors on the 80 mega chip
which we can configure in a set using code. You could also use a physical resistor here
but we're going to set it in code because it saves us a component and no extra no extra
hassle.
● The pull-up resistor value is quite high. It has a really large resistance and depends on
the chip that for example anything between a 10 kilo ohm resistor could walk through -
yeah say 100 kilo resistor or 100 ohm resistor and the reason it's so large is it allows
barely any current to flow but it still ties that pin to 5 volts now what happens is is when
the pin is not been when it's released it's time to affirm that the voltage v hull so when
the pin is not been pressed it will read a 1 or a 5 volt. Now when it is pressed that pin
goes directly to ground and follows the path of least resistance or straight to ground. It's
going to take ground and that means that it will register a zero because that resistor
value is quite high which means we get a positive voltage when it's not been pressed
and ground when it is been pressed a really firm definite voltage.
Figure 1.1 - Using Input Pullup Resistor with INPUT_PULLUP Method for Connecting Button with IC to
Arduino
● We're using active low logic meaning that when we activate the switch when we do
something with it it's a zero and when we release it when it's not active it's one which is a
little bit inverted to the intuitive way of thinking but by doing that allows us to use
a pull-up resistor inside the chip.
● We could also get an external resistor and use it as a pulldown resistor flip that
schematic that we were using but it introduces an extra component and it's not needed.
Active logic works very well; it is usually standard for things like buttons because what
resistors are standard on these chips.
● Now the goal is to turn the LED on when the button is not been pressed and off when it
is been pressed and the reason why this is really easy to do is because our uses what's
called active low logic meaning when it's being pressed it will output a zero because
when it is being pressed it will connect the pin to ground or a zero when it's not being
pressed.
Figure 1.2 - DigitalRead() and DigitalWrite()
● Create button state and we're going to use that to store the state of our button now we're
going to be using the method called Polling which means rather than triggering only
when the button is pressed, our loops iterating so quickly hundreds of thousands or
millions of times per second just going over and over and over again so that many times
per second it's going to check the state of our button and do something with it. Really
clean, really efficient either to use button states is going to be equal to the state of the
input pins we can store.
● The digitalWrite() [Digital I/O] writes a HIGH or a LOW value to a digital pin.
○ If the pin has been configured as an OUTPUT with pinMode(), its voltage will be
set to the corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V
(ground) for LOW.
○ If you do not set the pinMode() to OUTPUT, and connect an LED to a pin, when
calling digitalWrite(HIGH), the LED may appear dim. Without explicitly setting
pinMode(), digitalWrite() will have enabled the internal pull-up resistor, which acts
like a large current-limiting resistor.
● The digitalWrite() [Digital I/O] writes a HIGH or a LOW value to a digital pin.
○ Reads the value from a specified digital pin, either HIGH or LOW.
○ Syntax: digitalRead(pin)
Using Analogue Pins
● If a digital signal is 1 and a 0 (5 volts or 0 volts), then an analog signal is anything and
everything in between.
● The analogWrite() doesn’t output a varied voltage but what it does is it generates PWM
single pulse width modulation which tends to pin on and off hundreds of thousands of
times per second which turns the LED on and off faster than the human eye can see.
● ADC the converter which converts the analog signal into a digital signal is 10 bit, these
values between 0 and 1,023 the PWM however the hardware PWM is only 8 bits which
means it can only output varying varying values between 0 and 255 to 256 different
possible values so it gets 255 which it changed exactly as we expected to. But then the
rest of the potentiometer wouldn't register it because it's beyond the value of the PWM.
So we divided it by 4 which gives the scale that now our maximum value of height is
going to be 255 or thereabouts.
● The analogWrite() [Analog I/O] writes an analog value (PWM wave) to a pin. Can be
used to light a LED at varying brightnesses or drive a motor at various speeds. After a
call to analogWrite(), the pin will generate a steady rectangular wave of the specified
duty cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite())
on the same pin.
● The analogRead() [Analog I/O] reads the value from the specified analog pin. Arduino
boards contain a multichannel, 10-bit analog to digital converter. This means that it will
map input voltages between 0 and the operating voltage(5V or 3.3V) into integer values
between 0 and 1023. On an Arduino UNO, for example, this yields a resolution between
readings of: 5 volts / 1024 units or, 0.0049 volts (4.9 mV) per unit. See the table below
for the usable pins, operating voltage and maximum resolution for some Arduino boards.
○ The input range can be changed using analogReference(), while the resolution
can be changed (only for Zero, Due and MKR boards) using
analogReadResolution().
○ On ATmega based boards (UNO, Nano, Mini, Mega), it takes about 100
microseconds (0.0001 s) to read an analog input, so the maximum reading rate is
about 10,000 times a second.
Figure 1.1 - ATmega Based Boards
● The Serial Monitor is a separate pop-up window that acts as a separate terminal that
communicates by receiving and sending Serial Data. See the icon on the far right of the
image above.
● Serial Data is sent over a single wire (but usually travels over USB in our case) and
consists of a series of 1's and 0's sent over the wire. Data can be sent in both directions
(In our case on two wires).
● You will use the Serial Monitor to debug Arduino Software Sketches or to view data
sent by a working Sketch. You must have an Arduino connected by USB to your
computer to be able to activate the Serial Monitor.
Code Sample:
/* YourDuinoStarter_SerialMonitor_SEND_RCVE<br> - WHAT IT DOES:
- Receives characters from Serial Monitor
- Displays received character as Decimal, Hexadecimal and Character
- Controls pin 13 LED from Keyboard
- SEE the comments after "//" on each line below
- CONNECTIONS:
- None: Pin 13 built-in LED
-
- V1.00 02/11/13
Questions: terry@yourduino.com */
if(ByteReceived == '0')
{
digitalWrite(led,LOW);
Serial.print(" LED OFF");
}
● Serial dot begin a 9600 - serial is the built-in serial library you can get external software
serial library so you can use different pins which uses a huge amount of overhead
resources.
● https://www.instructables.com/HOW-TO-use-the-ARDUINO-SERIAL-MONITOR/
● Serial.begin(9600) doesn't actually print anything. For that you'd want to use
Serial.print("Hello world!") to print the text "Hello world!" to the serial console. Rather it
initializes the serial connection at 9600 bits per second.
IF Statements
● If statements are the backbone of decision making from microcontrollers and
systems they allow you to end up or bypass a section of code based on a condition or
set of conditions this could be as simple as checking to see if a variable is a 1 or a 0 or a
more sophisticated condition such as determining whether an array is sequence
correctly.
● The unsigned long int data type denotes a 32 – bit integer. It does not use a bit to store
the sign. Hence it can hold only positive values between 0 and 4,294,967,295 (2 32 – 1).
○ A variable modifier long essentially doubles the length of the in doubles the size
of the contains that have only been able to store 2 bytes it can store 4 bytes of
data in there which is really really handy and unsigned touches on something that
we haven't actually covered yet unsigned removes the possibility to negative
values within the integer you see within an int or any variable for that matter.
Figure 1.1 - Millis
● The millis is a software timer that is specific to Arduino. It's an Arduino function and
what it does is it records the amount of time that just transpired since boot or since the
program was initiated so you can use millis to keep track of time and specific things and
use that to update other variables of the software timer.
● Low pass filter if you're familiar with that and all it means is we're getting rid of the
noise. The high frequency noise that's happening and we only want the events when we
want to do something on the events that are happening at a lower frequency we're
passing the lower elements of the signal.
Figure 1.2 - Millis 1