I've recently developed a keen interest in hardware and microcontrollers and decided to document my journey. My learning method usually involves head-diving into a big project but this time I've taken a pragmatic approach and decided to acquire a solid foundation before moving onto bigger projects.
The Arduino environment is made up of the hardware itself, the Arduino board and shields, and the software, the Arduino IDE. Arduino Shields are add-on boards which can be stacked on top of the base Arduino board to extend its capabilites. A large range of shields exists, from Ethernet shields, to LCD shields, to RFID shields. We'll take a look at some of the shields in future posts. I'll be using the Arduino Uno in my examples but other Arduino boards can be used.
The Arduino UNO board contains the following components:
The Arduino IDE has everything required to get you started with coding for the Arduino. It can be downloaded from arduino.org.
It provides the following main features:
Follow the steps below to set up a working Arduino environment:
Every Arduino sketch must contain these 2 functions:
A microcontroller is a System on a Chip (SoC) on a single integrated circuit containing a processor, memory (RAM & EEPROM) and I/O pins. Essentially it's the heart and brains of the circuit; it accepts input from sensors, processes it and serves output to electronic components. Microcontrollers simplify circuits. Without them it would be very hard to create logic and changes in logic could mean drastic changes to the circuit itself. Let's start by migrating a traditional simple circuit to an Arduino simple circuit.
A simple circuit looks like this:
The closed circuit contains a power source, in our case 5V supplied by the USB, an LED and a resistor. The resistor prevents the LED from burning as the power can be overwhelming. You can solder these components together or use a solderless breadboard. The latter is a board for prototyping of electronics as it allows components to be easily connected in a non-permanent fashon. The one I have looks like this:
The holes of a breadboard are internally connected to each other in rows of 5 holes and columns along the sides. So, for example, if an end of a resistor is in hole 3F and an end of an LED is in hole 3J, they're connected. If one end is in 15A and the other is in 30A, they're disconnected. With this at hand, let's create the simple circuit:
Awesome isn't it ?? We now have a blinking light with no more complications than a simple circuit's.
We'll now take a look at one of the most simple input devices a circuit can contain, the push-button. A push-button can be 1 of 2 states, ON, when pressed, and OFF, when released. Let's extend our previous circuit to include this and light the LED only when the button is being pressed.
The schematic of the input part looks like this:
Wire the circuit in the following manner:
The Arduino Environment
Arduino Board
- ATmega328 Microcontroller - Main microcontroller; the only one that's programmable by the user.
- ATmega16U2 Microcontoller - Handles the communication with the USB; non-programmable.
- USB Connector - Used to transfer data and provides power to the board.
- Reset Button - Resets the device.
- Power Connector - Necessary when dealing with hardware which requires more power such as motors.
- Pins - The connection to the outside world.
- Digital I/O - Can be 1 (5V) or 0 (0V).
- Analog Input - Accept analog input ranging from 0V to 5V. Note that they do not provide analog output.
- Power/Reset - Provide power to a circuit.
- ICSP (In-Cirduit Serial Programming) - Used to update the bootloader/firmware of the microcontrollers.
Arduino IDE
- A coding environment to write sketches (Arduino Programs) in.
- Debugs, cross-compiles and uploads sketches to the ATmega328 microcontroller.
- A Serial Monitor used to debug sketches. We'll talk about this in the next part.
- Several example sketches to get you started.
Setting up the Arduino Environment
- Download and install the Arduino IDE.
- Connect the Arduino board to the computer.
- Start the IDE.
- Select your Arduino board from Tools -> Board.
- Select the COM port your Arduino is attached to from Tools -> Port.
- To verify everything is working, load the Blink sketch from File -> Examples -> Basics -> Blink, and hit Upload.
- After a few seconds, the LED next to pin13 should be blinking.
Anatomy of a Sketch
- Setup Function
- Executes once at start, when the Arduino board is powered up
- Used for initialisation
- Does not take any arguments and returns void
- Loop Function
- Iterative for as long as the Arduino is powered on
- Executes after the Setup function
- Contains the main logic of the program
- Does not take any arguments and returns void
The Leap to Microcontrollers
- Connect a wire from the 5V pin to 1J
- Insert a resistor in 1I and 7I
- Insert an LED in 7H and 12H
- Close the circuit by connecting 12I with ground
void setup() { pinMode(9, OUTPUT); } void loop() { digitalWrite(9, HIGH); }The code initialises pin9 as an OUTPUT pin and sets it to HIGH, allowing current to flow from this pin. Let's take this opportunity to introduce some basic, indispensable functions we'll be using in nearly every sketch.
- pinMode (pin, mode) - Initialise pin to a specific mode
- pin - The pin to be initialised
- Digital Pins - 0 - 13
- Analog Pins - A0 - A5
- mode - The mode of operation
- INPUT - Pin acts as a receiver
- OUTPUT - Pin acts as a transmitter
- INPUT_PULLUP - Pin acts as receiver with reverse polarity; HIGH becomes LOW and viceversa
- Example - pinMode(9, OUTPUT)
- pin - The pin to be initialised
- digitalWrite (pin, value) - Assigns a voltage to a pin
- pin - The pin in question
- value - Voltage to be sent: HIGH(5V) or LOW(0V)
- Example - digitalWrite(9, LOW)
- digitalRead (pin) - Returns state of an INPUT pin.
- pin - The pin in question
- Return Values - HIGH(5V) or LOW(0V)
- Example - int pinValue = digitalRead(9)
void setup() { pinMode(9, OUTPUT); } void loop() { digitalWrite(9, HIGH); delay(1000); digitalWrite(9, LOW); delay(1000); }This sketch sets pin9 to HIGH, waits for a second (delay(1000)), sets pin9 to LOW, waits for another second and repeats the process indefinitely. The result should like something like this:
Dealing with Digital Input
- Connect a wire from the 5V pin to 20J
- Insert a push-button in 20F and 22F
- Insert a resistor in 22H and 28H
- Close the circuit by connecting 28I with ground
- Connect the sensor wire to 22G and pin10
void setup() { pinMode(10, INPUT); pinMode(9, OUTPUT); } void loop() { if (digitalRead(10) == HIGH) digitalWrite(9, HIGH); else digitalWrite(9, LOW); }The setup function establishes that pin9 and pin10 are used as OUTPUT and INPUT pins, respectively. When pin10 is HIGH, i.e. the push-button is being pressed, pin9 is set to HIGH, i.e. the LED lights. When the switch is off, no current flows to pin10 and hence the LED is set to LOW, i.e. it's switched off. The sketch could be condensed to the following:
void setup() { pinMode(9, OUTPUT); pinMode(10, INPUT); } void loop() { digitalWrite(9,digitalRead(10)); }Andddd a video of it working cause why not:
No comments:
Post a Comment