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.
• 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?
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
PROGRAMMING 101 /* These comments describe code, but don't affect the output. */ void setup () { // This part runs once } void loop () { // This part runs infinitely }
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 >>
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
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.
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"); }
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); } }
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); }
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.
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); }
PPM FOR SERVO CONTROL #include 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); } }
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; } }
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; } } }