Arduino Robotics, Summer 2015, California College of the Arts

Arduino Robotics
Lecture: M-F 10AM – 1PM
Main building, San Francisco, room 107 (Hybrid Lab)
Spring 2015

Instructor: Michael Shiloh
mshiloh@cca.edu
Office hours: M-F 1:00PM-1:30PM or by appointment
Lab monitor: Zoe Murray
Lab: M-F 1:00PM-4:00PM

Syllabus: The syllabus is here.

Jump to current homework assignment

This page is http://teachmetomake.com/wordpress/arduino-robotics-summer-2015-cca

Course Objectives

The purpose of this course is to teach you the practical tools you will need to design and build robust and functional interactive electromechanical devices. The concepts and techniques you will learn in the context of robotics can be applied to a very wide range of projects.

This course will cover programming, electronics (basic circuits, Arduino, sensors and actuators), and construction techniques. Electronic and programming theory will be covered as necessary to support the practical needs of this class. The goal will be to give you the tools and the confidence to combine basic hardware, software, and mechanical building blocks (such as those taught in this class or those you might garner from the internet) into functional realizations of your ideas.

A strong theme of this class will be documentation. Obviously it’s important for your own studies, and it’s especially important when dealing with electronics and programming that become opaque very quickly. In addition, we all benefit tremendously from the projects others have shared freely on the internet and in other places, and it’s important to pay forward to this pool. As a side effect, you might receive excellent feedback and even recognition and fame for work you publish.

Finally, I want to make this class responsive to your needs and interests. Please ask questions, make suggestions, and tell me what you’d like to learn.

You are encouraged to be very broad, adventurous, and creative. You should take advantage of the freedom you’re given and craft your explorations to spread your wings and get the most from the experience, resources, and the support that are available at CCA in general and in this course in particular.

Learning Outcomes

  • Know how to conceptualize, design, and build a project involving some or all of sensors, actuators, Arduino,, and possibly other programming languages
  • Know how to interface integrated circuits (ICs) to Arduino
  • Understand the principle of an H-bridge, and know how to use it to control motors
  • Know how to program robotic behaviour, using inputs from sensors, making decisions based on those inputs, and then creating the desired behaviour by controlling outputs.
  • Know how to use a digital multimeter to measure voltage and resistance, and know how to use those measurements to confirm correct operation or to locate causes of incorrect operation
  • Know how to use advanced interfaces such as I2C and SPI to communicate with sensors and actuators that use those
  • Know how to chose the right motor for your project, and how to control it
  • Know how to design and construct circuits and software for robustness and ease of maintenance, repair, and modification
  • Know how to tap into an existing device for control by or input to for a larger project
  • Know how to document your project so others can understand, duplicate, and build on your work
  • Know how to research and understand topics beyond what is taught in class

Equipment

Required Equipment

  • Each of you will be given a kit of parts
  • Each of you must bring a laptop (Windows, Linux, or Macintosh). If you don’t have a suitable laptop it is your responsibility to check one out of the Media Center every day. You must do your own work and may not share with another student
  • Bring your laptop and the kit to every class

 Optional equipment

As you develop your projects and interests, you might need to purchase additional components and devices. This is impossible to predict as the range of projects you might approach are indeed infinite. At the low end, you can build amazing projects from discarded electronic devices such as printers at absolutely no cost; at the high end there is no limit; a complicated project could easily start at hundreds of dollars. Some lessons I’ve learned:

  • Projects will cost more than you think
  • Projects will take longer than you think
  • Things that you think should be easy will be hard
  • You will order parts that are wrong or that you simply decide not to use. Be ready to accept this. Consider these items you might trade with other students (or the larger maker community) for parts that you do need, especially when you need that part urgently.
  • You will spend less time and money if you are flexible about your concept. Allow prototype iterations to modify your concept, not just your execution. The reverse is also true: If you strongly want to stick to your concept, be prepared to spend more time and money. The common way of looking at this is to consider that there is a relationship between time, money, and features. You can choose any two of them, and the third will grow (or shrink) to accommodate.

June 1

Lecture

  •  Introduction and review of syllabus
  •  Installation and basic Arduino concepts using Arduino Hands-on Introductory Workshop (from Installation through analogWrite(): Controlling speed or brightness)
  • Digital Multimeter

Homework due June 2

  1. Create your online  which will be used for homework, lab reports, etc. You may use anything you like but it must be clear and legible. I recommend WordPress or Medium. Email me the URL.
  2. If you have taken a class from me before you do this tutorial instead
  3. Review what we did in class, as necessary. Here are some additional resources:
    1. Review Arduino basics by reading the Intro through Lesson 4 of Adafruit’s Arduino tutorial
    2. Review the AnalogReadSerial on the Arduino website. Remember that we used a photoresistor instead of the potentiometer.
    3. Review the photoresistor on Instructables
    4. Review the Arduino Fade tutorial. Note that the example is in File -> Examples -> Basics -> Fade.
    5. For another perspective, the Arduino Fade Tutorial is also described here.
    6. Excellent introduction to Arduino: Arduino in a Nutshell
    7. Arduino overview with links to many other sources of information
    8. There are endless resources on the web. Just Google the word “Arduino” along with whatever you are confused about.
  4. Learn about switches and digitalRead
    1. Perform the following steps, and describe the process on your website/blog/wiki/whatever in your words, accompanied by schematics, drawings (nothing fancy – simple hand sketches are fine), and programs, so take notes and save programs as you go along. It is very important that you discuss what didn’t work or what you didn’t understand. I want you to describe the process, not just the final results.
    2. Build the photoresistor circuit that we used in class, upload the sketch, open the serial monitor, and verify that the numbers change as you allow more or less light to fall on the photoresistor. Make note of the smallest and biggest numbers you get, very roughly (say rounded to the nearest 50). Remember that the diagrams I showed in class are here
    3. Remove the photoresistor, and in it’s place put a pair of wires that go off the breadboard and connect to nothing. In the picture and diagram below the wires are the white and blue ones on the left:20150128_182110arduinoWireSwitch_bbarduinoWireSwitch_schem
  1. Observe the numbers in the serial monitor. They should be close to zero. Does this make sense? Remember what I said about the voltage divider: the voltage in the middle of the divider (at the Analog Input pin A0) will be somewhere between zero and five volts, depending on the ratio of the two resistors. The fixed 10k ohm resistor is still there, but the photoresistor is gone and in it’s place are a pair of wires that aren’t connected to anything, in other words, an open circuit. The resistance of an open circuit is infinite, and compared to infinity, the 10k ohm resistor is very small, so it pulls the middle voltage down, close to zero.
  2. While watching the numbers in the serial monitor, touch the ends of the white and blue wires to each other. The numbers should jump up to close to 1023. Again, remember that the voltage in the middle of the divider (at the Analog Input pin A0) depends on the ratio of the two resistors. This time in place of the photoresistor we have a pair of wires that are connected to each other, in other words, a closed circuit, and specifically, a circuit with no resistance. Compared to a circuit with no resistance, the 10k ohm resistor is very big, so the closed circuit pulls the middle voltage up close to 5V.
  3. The blue and white wires are a switch. A switch simply makes a connection, in which case you have a circuit with no resistance, or opens the circuit, in which case you have an infinitely high resistance. Every switch is a variation on this basic principle.A switch can be either open or closed, resulting in a reading of close to zero or close to 1023. The Analog Input, which is designed for measuring voltages with many possible values, is wasted on this sort of input. Much more suited to a switch is a Digital Input, which is designed to only measure one of two values: LOW and HIGH.
  4. In the program, replace the analogRead() command with digitalRead() and upload the program. Close and open your switch, and see what you get in the serial monitor. You should see zero with the switch opened, and one with the switch closed. Zero represents LOW, and one represents HIGH.

June 2

Homework review

Lecture+lab

Take careful notes during class. Your homework will include writing a process report on what you did today.

  1. Conditional behavior
    Most interesting projects require making some decision, perhaps based on information from a sensor. This exercise will show you how to do that:

    1. Build this circuit on your breadboard. It should look familiar, it’s the same combination of two circuits we used yesterday. Note that we’re using pin 3:
      arduinoLDRLED_schem
    2. How would you make the LED come on only when the reading from the light sensor was above or below some value?
      if (sensorValue > 500) {
          digitalWrite(3, HIGH)
        }
        else {
          digitalWrite(3, LOW)
        }

      Note how the curly braces enclose things that are meant to be considered together.
      Note that indentation is to make it easier for us to read but does not affect the program at all. In this class, you are required to indent properly.

    3. Write a program to do this. Instead of writing a program from scratch, start with a related example and modify it. A good example to start with might be AnalogReadSerial (Files -> Examples -> Basics -> AnalogReadSerial).
      The rules for using if() are here, and the rules for using if()/else are here. Spend a few minutes studying them.You can use Serial.println() to tell you what’s going on:
      if (sensorValue > 500) {
          Serial.println("Value is greater than 500");
          digitalWrite(3, HIGH)
        }
        else {
          Serial.println("Value is less than 500");
          digitalWrite(3, LOW) 
      }

      When you get this working, save the program with a new name.

    4. Add another two LEDs on two other pins. You may use any two pins you wish. Remember that each LED must have its own resistor, and each LED needs its own pinMode() command.
    5. Test the two new LEDs by running Blink (Files -> Examples -> Basics -> Blink) three times, each time testing a new LED.
    6. Once you have your circuit working properly, draw the schematic for your circuit.
    7. Open the program you saved from step C and save it as a new program. Modify this program so that one LED turns on when the sensor reading is below some value (say 400), two LEDs turn on when the sensor reading is above this value, and all three LEDs turn on when the sensor reading is above a higher value (say 800). (The values I chose may not work for you – chose values that you can create conveniently by covering the photoresistor or shining your cellphone onto it) Save your program
  2. For() loops
    1. Open a new program and put this in the setup():
        Serial.begin(9600);
        int x;
        for (x = 5; x < 15; x = x + 1) {
          Serial.println(x);
        }

      Run the program and try to describe what it does
    2. For() loops are useful when you need to do something multiple times, making a slight change each time.
    3. Fading (Files -> Examples -> Analog -> Fading).

Homework due June 3

  1. Learn how to paste a program into your blog. In WordPress, you can use the “Preformatted” paragraph style. In Medium, select the program and type <APPLE> <ALT> 6. In the assignments below, include the program in your blog. Make sure your program is properly commented and indented.
  2. In your blog, describe what we did in class today. If you were not able to complete all the steps in class, do them for homework.
  3. Read about the Boolean Operators
  4. Using these boolean operators, modify the program you wrote in class in part 1 step G so that only one LED is on at a time: e.g. if the sensor reading is below 400 then only the first LED should be on, if the reading is between 400 and 800 then only the second LED should be on, and if the reading is above 800 then only the third LED should be on.
  5. Explain, in your own words, the difference between = and ==
  6. Attach three LEDs to your Arduino on adjacent pins (e.g. 3, 4, 5)
    1. Modify the Blink program so that each LED blinks in turn, using a for() loop. The LED pattern should be like this:
      LED 1 on
      LED 1 off
      LED 2 on
      LED 2 off
      LED 3 on
      LED 3 off

June 3

Homework review

  1. Arrays
    1. Arrays are like a spreadsheet. You identify each element by a number. Arrays are useful for storing a bunch of things with one variable name. This is allows you to access each element in an array inside a for() loop. Open a new program and put this inside setup():
      Serial.begin(9600);   int foo[5]; // foo is the name of the array // each element in the array is an int // there are 5 elements in this array     Serial.println("before initializing");   for (int i = 0; i < 5; i ++) {     Serial.println(foo[i]);   }     foo[0] = 5; // note that the first element is #0   foo[1] = 3;   foo[2] = 5.9; // pay attention to this   foo[3] = 54000;   foo[4] = 7; // note that the last element is #4       Serial.println("after initializing");   for (int i = 0; i < 5; i ++) {     Serial.println(foo[i]);   }

      Run the program and observe the results. Describe what happens. Try to explain why in your own words.
    2. Change the array type from int to long and run the program again. Explain why things changed.
    3. Change the array type from long to float and run the program again. Explain why things changed.
  2. Functions
    1. Functions are like robots with very specific tasks
    2. Function definition
      void printFoo() {
        for (int i = 0; i < 5; i ++) {
          Serial.println(foo[i]);
        }
      }
    3. Using a function
        Serial.begin(9600);
        int foo[5]; // foo is the name of the array
                    // each element in the array is an int
                    // there are 5 elements in this array
        
        Serial.println("before initializing");
        printFoo();
        
        foo[0] = 5;  // note that the first element is #0
        foo[1] = 3;
        foo[2] = 5.9; // pay attention to this
        foo[3] = 54000;
        foo[4] = 7; // note that the last element is #4
        
        
        Serial.println("after initializing");
        printFoo();
    4. Three different types of functions:
      1. No arguments; no return value
      2. One or more argument
        void printProduct(int a, int b) {
          int c;
          c = a * b;
          Serial.println(c);
        }
         
        void setup() {
          Serial.begin(9600);
          printProduct(5, 9);
        }
      3. Return value (only one value)
        int getProduct(int a, int b) {
          int c;
          c = a * b;
          return(c);
        }
         
        void setup() {
          Serial.begin(9600);
          int a = getProduct(5, 9); 
          Serial.println(a); 
        }
    5. Reasons for functions:
      1. Encapsulate code that is used more than once
      2. Write programs that are easier to understand
  3. Soldering demonstration and practice
    1. Wire to wire
    2. Solid vs. stranded wire
    3. Stranded wire to LED with solid wire tips and heat shrink tubing

Homework due June 4

  1. Read the Arduino array tutorial
  2. Connect 3 LEDs to three non-adjacent pins (e.g. 3, 12, 7) on your Arduino. Make the LEDs blink in turn like you did in step #6 yesterday. Use for() loops and an array.
  3. Read the Arduino function tutorial
  4. Write a function that turns off the three LEDs in problem #1, and modify your solution to problem #1 to use this function. In the loop, use the function to turn off all LEDs, then turn on only the one LED that should be on.
  5. Read about transistors here
  6. Read Arduino outputs: Voltage and current and Controlling large loads with a transistor
  7. Read the tutorial and watch the videos about H-bridges:
    1. www.mcmanis.com/chuck/Robotics/tutorial/h-bridge/
    2. https://www.youtube.com/watch?v=iYafyPZ15g8
    3. http://youtu.be/Te5YYVZiOKs
  8. Read the Arduino Potentiometer tutorial. The potentiometers we have in the lab look like this (also called trimpots) and can be inserted directly into your solderless breadboard like this.

June 4

Homework review

Finish soldering demo and practice

  • Wire to male header pins (practice)
  • Short solid core wire to female header pins (for keeping)
  • Stranded wire to motor and male header pins (for keeping)

Lecture and lab

  • The difference between voltage and current
  • The relationship between voltage and current
  • Transistor theory
  • Transistor lab (skipped)
  • How to reverse the direction of the motor: H-bridge

Show robot
Homework due June 5

  1. Explain voltage and current in your own words
  2. Finish soldering wires to two of your motors. Attach male headers on the other end.
  3. Do H-bridge on a breadboard lab
    • With switches
    • With Ardiuno
  4. Start building your robot

June 5

Homework review

Lecture

  1. Arduino schematic
    1. Difference between Vin and 5V
    2. Why avoid (digital) pins 0 and 1

Demo and lab

  1. Soldering on a perforated breadboard
  2. Build H-bridge on a perforated breadboard (don’t forget socket) according to this schematic:
    l293d_vin_schem

Homework due June 8

  1. Finish soldering H-bridge on a perforated breadboard (don’t forget socket)
  2. Finish building robot
  3. Test both robot wheels. Because you’ve only one H-bridge, you’ll have to test the wheels one at a time. Make sure both wheels go  forwards and backwards.
    • To enable motor to go: Enable = HIGH
    • To turn one way: In 1 = HIGH, In 2 = LOW
    • To turn the other way: In 1 = LOW, In 2 = HIGH
    • Include your program in your blog
  4. Test that you can change the speed of the motor. Instead of setting Enable to HIGH, use analogWrite() to set it to some intermediate value. Include your program in your blog

June 8

Homework review

Demonstrate robot chassis

Lecture

  1. Arduino schematic
    1. Why avoid (digital) pins 0 and 1 (I forgot to mention this on Friday)
    2. Power distribution
      1. How 5V is generated from Vin
      2. Voltage drops in non-ideal conductors
      3. What happens to Vin when a motor draws a lot of current and why the microcontroller chip resets continuosly
      4. Solution: separate battery for motor (via Vin) and logic (via USB connector)
  2. Autonomy:
    1. light seeking using two light sensors
      1. lab: add two light sensors; turn motor on or off (or forward/backwards) depending on which light sensor has the greater reading

Homework due June 9

  1. Finish lab
  2. Install the programming language called “Processing” from processing.org

June 9

Lecture

  1. Arduino shields
  2. Arduino Prototyping Shield

Demo

  1. How to build the h-bridge on the Arduino Prototyping Shield

Lab

Homework due June 10

  1. Build the dual h-bridge on the Arduino Prototyping Shield
  2. Make a light seeking robot

June 10

Announcements

  1. I may be gone the last three days of class; I have arranged for an excellent teacher to take my place.
  2. Field trip to a local electronics factory (Seeed Studio) Jun 18 at 14:00 PM?

Lecture

  • Sensors. There are too many to list but some categories:
    • Collision detection
      • Switch
        • Store bought
        • DIY
    • obstacle avoidance
      • Distance measuring
        • IR
        • Ultrasonic
        • Exotic (laser, radar)
    • Self preservation
      • how not to fall off the table
        • Surface detection
          • Distance measuring
          • Reflective
            • DIY
            • Store bought (often called “reflective” or “proximity sensor”)
    • Student generated list of sensors
  • Measuring distance
  • How to graph data: use Processing
  • What to do about noisy data
  • What to do about noisy electrical signals

Lab

  1. Do the HC-SR04 tutorial (just the first part)
  2. Do the smoothing tutorial (use a photoresistor instead of the potentiometer)
  3. Combine the smoothing tutorial with the Arduino/Processing graph example

Homework due June 11

Finish the lab and describe it on your blog

June 11

Lecture: How to use sensors

  1. Resistive sensors: use voltage divider circuit
  2. Analog output sensors: connect directly to analog input
  3. Other sensors: must research the sensor to understand how to use it

Demo/lab

Reflective sensor to avoid falling off the table (also useful for line following robot)

  • Research your sensor (this is a good way to start with any new sensor)
  • Pololu QTR-1RC product description
  • Follow link to Arduino library, download zipfile and install it like this:
    1. Identify your sketchbook location ( File -> Preferences). Open a finder window to this folder, and then to the libraries folder within.
    2. Close Arduino
    3. Unzip the library and open a finder window to the created folder (qtr-sensors-arduino-master)
    4. In qtr-sensors-arduino-master is a folder called QTRSensors. Move QTRSensors to your library folder identified in step 1 above
    5. Open Arduino
    6. File -> Examples -> QTRSensors -> QTRRCExample
    7. Upload and open Serial Monitor
    8. Calibrate by covering and uncovering the sensor
    9. When pin 13 LED goes out, test the sensor

Accelerometers

  • Easy in comparison: it’s just an analog voltage so use analog input

Homework due June 12

Using the ultrasonic distance measuring sensor, try to make your robot avoid obstacles

June 12

Homework review

  • Test ability of robot to avoid obstacles

Lab

Today will be an open ended lab to allow time to integrate the concepts we’ve learned in the past week. Here are some suggested goals:

  1. Program robot seeks light
  2. Program robot to avoid obstacles
  3. Program robot to seek light and avoid obstacles at the same time

Homework due June 15

  • Program robot to seek light and avoid obstacles at the same time

June 15

Announcements

  1. Field trip on Thursday
  2. Chris Cerrito on Tuesday
  3. North on Tuesday

Robot review

Wireless overview (quickly)

  • Replacing wire (USB or otherwise)
    • XBee
    • Cheap radio modules
    • Bluetooth
      • Hacking existing BT devices
      • Phone? You have to write your own Android/iOS application (very difficult)
    • BLE
      • BLE shield
      • Blend Micro (Arduino + built-in BLE shield)
      • Phone? You have to write your own Android/iOS application (very difficult)
  • WiFi
    • WiFi shields
    • Yun
  • Internet
    • Interface via a web server
    • Storing and retrieving information from the cloud (Parse)
    • Using public APIs (Temboo)
    • Twilio
    • IFTTT
  • Issues
    • Peer-to-peer WiFi (one reason the cloud is so important)

Introduction to wireless with the Arduino Yun

  1. Overview
  2. Things to be aware of
  3. Connect to your Yun via WiFi using this
  4. Read inputs and write outputs using a URL
  5. Create a web page that tells you what the pins are doing

June 16

Smaller Arduino style controller

  1. Upload: File -> Examples -> ArduinoISP
  2. Connect ATtiny to Arduino according to schematic hereDon’t forget capacitor
  3. Install support for ATtiny by following the instructions for “Installing ATtiny support in Arduino 1.6.4” on this page
  4. Open Blink example
  5. Change 13 to 0
  6. Open Tools -> Board and select ATtiny
  7. Open Tools -> Processor and select ATtiny85
  8. Open Tools -> Programmer and select Arduino as ISP
  9. Upload sketch
  10. Disconnect 13, 12, 11
  11. Connect an LED (with resistor) to the ATtiny on pin 5. It should blink.
  12. Other resources
    1. ATtiny85 and ATtiny45 pinouts at the bottom of this page
    2. How to program ATTiny with Arduino
    3. ATTiny project including installation and programming

Return to Arduino Uno schematic: What is meaning of all those letters on microcontroller? A brief overview of the ATmega328 datasheet

Links to student blogs or websites

 

Homework due

Print Friendly
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Leave a Reply

Your email address will not be published. Required fields are marked *