Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Intro to Arduino - RPI Embedded Hardware Club (...

Theo Pak
October 10, 2013

Intro to Arduino - RPI Embedded Hardware Club (10 OCT 2013)

RPI Embedded Hardware Club
http://rpiEHC.org

Intro to Arduino Workshop
Held October 10, 2013 @ 7-9pm in Sage 2202
Open to campus

Contact Theo Pak
[email protected]

Theo Pak

October 10, 2013
Tweet

More Decks by Theo Pak

Other Decks in How-to & DIY

Transcript

  1. EMBEDDED HARDWARE CLUB We are a non-profit organization made of

    students at Rensselaer Polytechnic Institute who share a passion for microcontrollers, electronics, and programming.
  2. • Easier to learn than alternatives. • Widely available for

    cheap! • Tons of code already exists. • Generally appropriate for real-time. • Other options have better specs. • Other options use lower power. WHY USE AN ARDUINO?
  3. TYPES OF ARDUINOS Due $50 32-bit ARM tons of GPIO

    Yún $70 dual processors Linux, WiFi, etc. o $20 2u4 e GPIO
  4. INSTALL THE ENVIRONMENT An Integrated Development Environment includes a code

    editor, compiler, and debugger. You need all three! The Arduino Team makes an IDE. Download it at http://arduino.cc
  5. PROGRAMMING 101 /* These comments describe code, but don't affect

    the output. */ void setup () { // This part runs once } void loop () { // This part runs infinitely }
  6. SELECT THE RIGHT BOARD Auto Format Archive Sketch Fix Encoding

    & Reload Serial Monitor Board Serial Port Programmer Burn Bootloader Arduino Uno Arduino Duemilanove Arduino Diecimila Arduino Nano /atmega328 Arduino Something else Arduino Mega Arduino Leonano Powahduino Tools >>
  7. SELECT THE RIGHT PORT Tools >> Auto Format Archive Sketch

    Fix Encoding & Reload Serial Monitor Board Serial Port Programmer Burn Bootloader COM3 COM4 COM6 /dev/ttyUSB1 /dev/ttyCOM1 So many Arduinos Arduino Leonano
  8. UPLOAD A TEST PROGRAM! The Arduino Software is an IDE.

    Use it to write your code. Press the buttons to compile or compile+upload.
  9. UPLOAD A TEST PROGRAM! Press the buttons to compile or

    compile+upload. The arrow button is "Compile+upload." That's how you run programs on your Arduino.
  10. HOW TO PRINT A MESSAGE void setup () { //

    Begin serial communication // at 9600 BAUD Serial.begin(9600); } void loop () { // Print the line "hello world" Serial.println("hello world"); }
  11. It's not made of food! Breadboards let you quickly build

    a circuit without using any tools. WHAT'S A BREADBOARD?
  12. FLASH AN LED void setup () { pinMode(13,OUTPUT); } void

    loop () { // Turn LED on digitalWrite(13,HIGH); delay(100); // Wait 100 milliseconds // Turn LED off digitalWrite(13,LOW); delay(100); // Wait 100 milliseconds }
  13. PUSHBUTTONS AS INPUT void setup () { pinMode(13,OUTPUT); // Configure

    Pin13 as output pinMode(5,INPUT); // Configure Pin5 as input } void loop () { // If the button is pressed... if (digitalRead(5)) { // ...then turn LED on digitalWrite(13,HIGH); } else { // ...if not, the turn LED off digitalWrite(13,LOW); } }
  14. POTENTIOMETERS AS INPUT void setup () { // Configure the

    serial monitor Serial.begin(9600); // We'll use the default settings for Analog1 } void loop () { // read the 10 bit value from Analog1 int value = analogRead(1); // write the value to serial Serial.println(value); }
  15. MORE ABOUT ANALOG INPUT void setup () { Serial.begin(9600); //

    Begin serial communication pinMode(A0,OUTPUT); // A0 (Pin14) digitalWrite(A0,LOW); // Start Pin14 LOW pinMode(A2,OUTPUT); // Pin A2 (Pin16) digitalWrite(A2,HIGH); // Start A2 HIGH } Analog pins 0-5 can be used as digital pins, if configured. This trick makes hooking up the pot easier in this example.
  16. NOW BACK TO POTS void setup () { Serial.begin(9600); pinMode(14,OUTPUT);

    digitalWrite(14,LOW); pinMode(16,OUTPUT); digitalWrite(16,HIGH); } void loop () { // read the 10 bit value from Analog 1 int value = analogRead(1); // write the value to serial Serial.println(value); }
  17. PULSE-WIDTH MODULATION void setup() { pinMode(3,OUTPUT); } void loop() {

    // send pulses to pin 3 // 128 of 256 should be high analogWrite(3,128); }
  18. PWM WITH FADING void setup() { pinMode(3,OUTPUT); } void loop()

    { for (int i = 0; i < 256; i++) { // send pulses to pin 3 analogWrite(3,i); // delay each loop delay(100); } }
  19. PPM FOR SERVO CONTROL #include <Servo.h> Servo myservo; //create servo

    object to control a servo void setup () { myservo.attach(9); //attach pin 9 to the servo object } void loop () { for (int i = 0; i < 255; i ++) { // set the servo position myservo.write(i); // wait for the servo to get there delay(15); } }
  20. SERIAL INPUT String inputString = ""; // a string to

    hold incoming data boolean stringComplete = false; // whether the string is complete void setup() { // initialize serial: Serial.begin(9600); // reserve 200 bytes for the inputString: inputString.reserve(200); } void loop() { // print the string when a newline arrives: if (stringComplete) { Serial.println(inputString); // clear the string: inputString = ""; stringComplete = false; } }
  21. SERIAL EVENT void serialEvent() { while (Serial.available()) { // get

    the new byte: char inChar = (char)Serial.read(); // add it to the inputString: inputString += inChar; // if the incoming character is a newline, set a flag // so the main loop can do something about it: if (inChar == '\n') { stringComplete = true; } } }