Engineering and Coding with Arduino
Total Page:16
File Type:pdf, Size:1020Kb
M Engineering and Coding with Arduino A Beginner’s Guide A PUBLICATION OF MAKECRATE TABLE OF CONTENTS Introduction 1 Microcontrollers 2 Getting Started 3 Some Beginner Projects 4 Additional Resources Introduction This guide is designed to introduce beginning makers to using microcontrollers like the Arduino platform to learn basic coding by utilizing engaging, hands-on projects with real world applica- tions as a foundation for introducing fundamental coding concepts. In this guide, you’ll get: • an introduction to microcontrollers and Arduino in particular • an overview of the skills you can learn when building and coding with microcon- trollers • guidance on getting started with microcontrollers • three beginner projects including illustrated circuit diagrams and code Chapter One Microcontrollers M WHAT IS A MICROCONTROLLER? • A microcontroller is a small, single-chip computer used to connect to and control another device. A typical microcontroller consists of some or all of these parts: • Central processing unit (CPU): The CPU is the brains of the microcontroller. Its job is to find the instructions in memory and decode them to make them usable by the microcontroller. • Memory: the microcontroller instructions as well as variables and their changing values get stored in memory and accessed by the CPU when needed. • Ports: Microcontrollers generally have both input and output ports where devices like sensors, LEDs, and displays can be attached. • Timers and Counters: Most microcontrollers have built in timers that provide clock function- ality and can control the timing of internal and external events, like the length of time an LED blinks. • Interrupt Controls: microcontrollers have systems called interrupt controllers in place that al- low the CPU to check which devices might need attention while another program is executing. • Analog to digital converters: a microcontroller’s analog to digital converter allows it to take an- alog data, like temperatures or light readings, and convert them to digital values that the CPU can handle. • Digital to analog converters: Similarly, digital info from the microcontroller may need conver- sion to run an analog device like a DC motor, so microcontrollers have converters to perform this function. WHERE ARE MICROCONTROLLERS USED? Microcontrollers are used in many electronics devices today including devices that measure, store, calculate, or display information. Some places you are likely to come across a microcontroller on a daily basis are: • In your kitchen, running the timer in your microwave or controlling the temperature in your oven or refrigerator. • In your living room, controlling your TV. • On your phone, to control the touchscreen. • In your car. Most modern cars contain at least 25 different microcontrollers! WHAT IS ARDUINO? Arduino is an open-source microcontroller that can be used with a large number of inputs and outputs to create anything from a simple blinking LED to a complex scientific device. The software that controls the Arduino is also open-source, available on most operating systems, and easy to use. An Arduino microcontroller used to control three LEDs. WHAT CAN YOU LEARN WITH MICROCONTROLLERS? Microcontrollers and the inputs and outputs that can be used with them provide fertile ground for explora- tion and learning. Here we outline a few of the things you can expect to learn. Circuit Building Every project starts with a circuit. The inputs and sensors needed to be wired properly in order to function. Through constructing simple circuits, students will gain a basic understanding of how electricity flows, how various components like resistors and diodes work, and how both analog and digital sensors measure infor- mation. Coding Every project you build will require a program to run it. Arduino uses a subset of the C++ coding language in its Integrated Development Environment (IDE) so you will get to learn that language. More important- ly, you’ll explore the logical structures that are used in many other computer languages, providing a strong foundation for learning additional languages in the future. You’ll also learn how to troubleshoot, as you write and change code and see how things go wrong. Connections to Science One of the best things about microcontrollers like Arduino is that they allow for connections between computer science, electronics, and various other sciences to be made naturally, in an entertaining manner. Using distance sensors to collect measurements reinforces an understanding of things like the speed of sound, and how to convert units. Wiring and coding an RGB LED helps with an understanding of the rela- tionship between color and light. Accelerometers and gyroscopes can be used to demonstrate principles from physics. The possibilities are nearly endless! Everybody in the country should learn to program a computer...because it teaches you how to think. Steve Jobs, Founder and CEO of Apple Chapter Two Getting Started M BUYING AN ARDUINO There are a number of options available directly from Arduino as well as through merchants like SparkFun and Amazon. Because Arduino is open-source, there are a number of inexpensive clones available that will work in a manner similar to a genuine Arduino as well. The Arduino Uno and its clones are recommended as a good starter microcontroller, and the projects in this guide are built on that microcontroller. An internet or Amazon search for Arduino Uno should provide a number of options; just be sure that the one you choose utilizes the ATmega328P microchip. You’ll also need a USB A to B cable to attach your Arduino to your computer. • USING THE SOFTWARE Downloading Drivers Standalone IDE Web editor If you have a reliable internet con- nection, the Arduino web editor is a great place to write sketches (the Arduino term for computer pro- grams) and store them in the cloud. You can download the code from the web editor to your Arduino or clone using a USB cable. The Arduino Web Editor You can find the web editor at create.arduino.cc. You can create an account and the website will walk you through the steps you need to get the proper drivers installed on your computer. Note that if you plan to use a number of computers, the drivers will need to be installed on all of them. The Online Development Environment Standalone IDE If you’d prefer to be able to use the IDE off-line, you can download and install a standalone version of the software. You’ll need an internet connection throughout the installation process, but not to write and run your sketches after that. Arduino provides step-by-step instructions for the installation process at https:// www.arduino.cc/en/Guide/HomePage. Download the Development Environment Chapter Three Beginning Projects M SIMPLE LED CIRCUIT Getting an LED to blink on and off is a simple way to get started with microcontrollers. Here we’ll build a simple circuit and play with the code to make the LED blink at different speeds. Parts You’ll Need • Arduino microcontroller • Breadboard • Jumper wires • 1 LED • 1 100 Ohm or 220 Ohm resistor Building the Circuit Use the diagram below to construct a circuit. Be sure that the longer leg on your LED is on the right side of the center channel on your breadboard. Writing the code // This is the code to make an LED blink on and off. const int ledPin = 13; void setup() { pinMode(ledPin,OUTPUT); } void loop() { digitalWrite(ledPin,HIGH); delay(1000); digitalWrite(ledPin,LOW); delay(1000); } SIMPLE LED & BUZZER CIRCUIT We’ll add on to the first project by building a circuit with two LEDs and a buzzer and get them to blink and buzz in an alternating pattern. Parts You’ll Need • Arduino microcontroller • Breadboard • Jumper wires • 2 LEDs • 1 100 Ohm or 220 Ohm resistor • 1 Piezo Buzzer Building the Circuit Use the diagram below to construct a circuit. Be sure that the longer legs on your LEDs are on the right side of the center channel on your breadboard. Do the same for the leg of the buzzer that has the “+” sign above it. Writing the code const int ledPin = 13; const int ledPin2 = 12; const int buzzerPin = 7; void setup() { pinMode(ledPin,OUTPUT); pinMode (ledPin2, OUTPUT); pinMode (buzzerPin, OUTPUT); } void loop() { digitalWrite(ledPin,HIGH); delay(1000); digitalWrite(ledPin,LOW); digitalWrite(ledPin2, HIGH); tone(buzzerPin, 35); delay(1000); digitalWrite(ledPin2,LOW); noTone(buzzerPin); } CONNECTING LIGHT AND SOUND We’ll use light measurements to create different tones in this project. Parts You’ll Need • Arduino microcontroller • Breadboard • Jumper wires • 1 10K Ohm Resistor • 1 Piezo Buzzer • Photoresistor Building the Circuit Use the diagram below to build your circuit, being sure to keep the positive side of the buzzer to the right. Writing the code int analogPin = A0; int noteToPlay; int sound; int speaker = 7; void setup() { Serial.begin(9600); pinMode(analogPin, INPUT); } void loop() { sound= analogRead(analogPin); delay(200); int notes[21] = {65, 73, 82, 87, 98, 110, 123, 131, 147, 165, 175, 196, 220, 247,262, 294, 330, 349, 392, 440, 494}; noteToPlay= map(sound, 0,1023, 0, 21); tone(speaker, notes[noteToPlay]); delay(10); } Chapter Four Additional Resources Now that you’ve got a taste for the power of microcontrollers for learning coding and engineering, here are some additional resources to help you learn more. M RESOURCES Arduino Project Hub The Arduino Project Hub (https://create.arduino.cc/projecthub) has lots of great user-submitted projects that are organized by category and difficulty. Instructables Instructables contains a wealth of user-generated content on DIY topics of all kinds, and has an entire sec- tion dedicated to Arduino projects including online classes. https://www.instructables.com/howto/arduino/ MakeCrate MakeCrate has several free projects on their website (www.makecrate.club) and provides monthly sub- scriptions including parts, videos, and downloadable step-by-step instructions to get you engineering and coding in no time! READY FOR MORE? Go to www.makecrate.club and sign up for a subscription today! Use code MCDOWNLOAD to save 20% today!.