Coding and Robotics Summary Arduino PDF
Document Details
Uploaded by CapableTsavorite
Curro
Tags
Summary
This document provides a summary of coding and robotics using Arduino. It covers introductory concepts and programming examples, including hardware components and software tools.
Full Transcript
Coding and Robotics Summary: ARDUINO 1 Introduction to the Arduino Uno What is the Arduino Uno? Arduino Uno is a microcontroller board based on the ATmega328P chip. It's one of the most popular and widely used Arduino boards. Designed for beginners and students, it is also ve...
Coding and Robotics Summary: ARDUINO 1 Introduction to the Arduino Uno What is the Arduino Uno? Arduino Uno is a microcontroller board based on the ATmega328P chip. It's one of the most popular and widely used Arduino boards. Designed for beginners and students, it is also very popular with advanced users because it's versatile and easy to use. Technical Specifications: 1. Micro-controller chip: ATmega328P Operating Voltage: 5V Input Voltage (recommended): 7-12V Input Voltage (limits): 6-20V Digital I/O Pins: 14 (of which 6 provide PWM output) Analog Input Pins: 6 DC Current per I/O Pin: 20 mA Flash Memory: 32 KB (0.5 KB used by bootloader) SRAM: 2 KB EEPROM: 1 KB Clock Speed: 16 MHz 2. Operating Voltage: The board operates at 5 volts, which is suitable for most electronic components and sensors. 3. Input Voltage: Recommended input voltage range is 7-12V. 4. Digital I/O (Input/Output) and Analog Output Pins: Arduino Uno has 14 bi-directional digital input/output pins, labeled 0 to 13. Six of these pins can also provide PWM (Pulse Width Modulation) analog output. 2 5. Analog Input Pins: Arduino Uno provides 6 analog input pins, labeled A0 to A5. These pins can read analog signals from sensors and other devices. 6. Memory: Flash Memory: 32 KB, where sketches (Arduino programs) are stored. SRAM: 2 KB, used for variables and runtime memory. EEPROM: 1 KB, for long-term data storage. 7. Clock Speed: The ATmega328P chip operates at a clock speed of 16 MHz, providing fast and efficient processing. 3 Arduino Arduino is a tool for controlling electronics. We program the pins to either read voltages or to give voltage. This way we turn devices on or off. HARDWARE Arduino Uno board The PINS on the boards POWER PINS: 5V – Positive connections – Send 5v to device connected to it 3,3v – Positive connections – Send 3,3v to device connected to it GND – Negative connections – Ground your circuit PWR IN – Plug in the power from batteries. USB – Connection to your computer to give some power and to send Data TO and FROM 4 RESET – Restarts the program loaded ANALOG INPUT - Read analog sensors – Returns any value read – not only 0 or 1 DIGITAL I/O – Only O or 1 – High or Low VOLTAGE. PWM - Arduino Uno R3 has 6 PWM pins that are 3, 5, 6, 9, 10, and 11. These pins are marked with the negation sign “ ~ “. These pins can generate a pulse as per the given inputs. PWM is used to control the amount of power delivered to the load. It is commonly used for controlling the brightness of LED, the speed of motors, etc. Digital - Input & Output You’ll use digital pins to read data from some components (sensors) and write data to other components (actuators – Motors or LEDs). A digital pin can have only 2 states: LOW or HIGH. You can consider them as binary pins. LOW means that the voltage on the pin is 0V. HIGH means Vcc, which is 5V send to Arduino Uno. Before you can actually use a digital pin, you need to configure its mode. A digital pin can either be on INPUT more or OUTPUT mode. When in INPUT mode, you’ll use it to read data. When in OUTPUT mode, you’ll use it to write data. Analog - Input An analog pin is useful to read values that can’t be just 0 or 1. This is typically used to read analog sensors. It will receive an input voltage and read this voltage. Let’s say the pin reads 3V. Then, an ADC (Analog Digital Converter) will change that analog value into something your Arduino program can understand – a digital value such as 614. 5 The BreadBoard A breadboard is a solderless construction base used for developing an electronic circuit and wiring for projects with microcontroller boards like Arduino. The DRV8833 motor driver This dual H-bridge motor driver allows speed and direction control of two DC motors at the same time. IR SENSOR The IR sensor has 2 LEDs on it. The Power LED will be on continuously when there is power on it. Your Signal LED will only light up when it is on white. Use a Phillip screwdriver and set the Potentiometer so that this is the case 6 SOFTWARE Arduino IDE (Integrated development environment) 7 Coding concepts to understand We use functions to give instructions through programming to the Arduino board. On the Arduino we code in C++ programming language. We use these functions within methods. void setup () method: pinMode ( ) – To configure the pins. We have to tell the Arduino board if a pin is either input or output. void loop () method: digitalWrite ( ) – To write information to digital pins. Used on OUTPUT devices such as LED’s or MOTORS. We write either HIGH or LOW. HIGH is to send voltage and LOW to take voltage away. digitalRead ( ) – To read information from digital pins. Used on INPUT devices SUCH as sensors. We will receive a reading of either HIGH or LOW (1 or 0). analogRead ( ) - To read information from analog pins. Used on INPUT devices SUCH as sensors. We will receive a precise reading that is not only 0 or 1. if statements ( ) – If statements are for an electronic device to make decisions based on information from the environment. serial communication - To print information received from sensors to a screen. Used for communication between the Arduino board and a computer or other devices. Serial Communicates through digital pins 0 (RX) & 1 (TX) and via USB to Computer for Sketches Arduino Digital I/0 CODE EXAMPLES pinMode(pin, mode) Sets pin to either INPUT or OUTPUT e.g. pinMode (5, OUTPUT) digitalRead(pin) Reads HIGH or LOW from a pin e.g. digitalRead (6) This will read data from pin 6 and return a 1 (High) or 0 (Low) digitalWrite(pin, value) Writes HIGH or LOW to a pin e.g. digitalWrite (5, HIGH) This will turn on which ever device is in pin 6 8 Arduino Analog Input CODE variable=analogRead(sensorPin); This will save a value it reads into a variable e.g. sensorValue = analogRead (A3) NB INPUT VS OUTPUT Input is a signal / information going into the board. Examples: Buttons Switches, Light Sensors, Flex Sensors, Humidity Sensors, Temperature Sensors… Output is any signal exiting the board. Examples: LEDs, DC motor, servo motor, a piezo buzzer, relay, an RGB LED Comments Comments are for you – the programmer and your friends…or anyone else human that might read your code. // this is for single line comments // it’s good to put a description at the top and before anything ‘tricky’ 9 Programming principles Programming is a language just like English. Syntax, capitals and small letters are important. Check semicolons Check brackets Check capitals and small letters Looking at sample code: 10 Variables Box to hold information Rules for creating a variable: Only one piece of information Need to give the box a name Also tell it what type of information it will hold Then give it information to hold int IN1 = 2; First, we create a box that will hold an integer. Then we call that box IN1 Then we put the value of 2 into that box 11 Serial Write – Key Concepts Serial.begin(): Sets the data rate in bits per second (baud) for serial data transmission Syntax: Serial.begin(baud) Example: Serial.begin(115200) sets serial baud rate to 115200 bits per second Serial.print(): Prints data to the serial port as human-readable ASCII text without carriage return / Newline Feed character Syntax: Serial.print(val) or Serial.print(val, format) Parameters: val: the value to print - any data type format: specifies the number base or number of decimal places Example: Serial.print(”Hello world.“) gives "Hello world.“ Example: Serial.print(1.23456, 2) gives “1.23” Serial.println(): Prints data to the serial port as human-readable ASCII text followed by a carriage return and a newline character Syntax: Serial.println(val) or Serial.print(val, format) Parameters: val: the value to print - any data type format: specifies the number base or number of decimal places Example: Serial.println(”Hello world.“) gives "Hello world.“ Example: Serial.println(1.23456, 2) gives “1.23” Serial.write(): Writes binary data to the serial port. This data is sent as a byte or series of bytes Syntax: Serial.write(val) Example: Serial.write(”Hello world“) writes "Hello world“ 12 Typical setup of LED lights to blink The RED LED will go on first for 2 seconds, after that the GREEN LED will go on for 2 seconds and the RED LED will turn off. After these 2 seconds the BLUE LED will go on for 2 seconds and the GREEN will go off. There after the BLUE LED will turn off and the RED LED must go on again. This will be repeated the whole time. 13 CONDITIONS To make decisions in Arduino code we use an ‘if’ statement ‘If’ statements are based on a TRUE or FALSE question VALUE COMPARISONS GREATER THAN - a > b LESS THAN - a < b IT IS EQUAL - a == b GREATER THAN OR EQUAL - a >= b LESS THAN OR EQUAL - a