Novice Lesson 1

Class structure

This lesson plan is designed for students as young as 10, and adults with little to no programming and electronics experience. The class, as structured here, should take about 1.5 – 2 hours, and will give the students a grounding in arduino basics, programming for arduino, using digital inputs and outputs, analog inputs and basic servo motors. This lesson plan assumes that you (the instructor) know the basics of arduino code, and electronics.

Setup

Before the class arrives, set up a computer for each pair of students. Download and install the arduino sdk on each computer. Compile and upload the following code to each board to confirm that everything is working as expected. It should blink the white LED once on power-on.

#define LED 6

void setup() {
   pinMode(LED, OUTPUT);
   digitalWrite(LED, HIGH);
   delay(500);
   digitalWrite(LED, LOW);
}

void loop() {
    delay(100);
}
									


Troubleshoot any issues on each computer, then unplug the arduino, and codeshield from each computer, leave them separate, but leave the USB cable plugged into the computer.

Open a new arduino SDK window on each computer, and paste in the following code:

/* Diyode CodeShield Base Script */
#define ENCODER_A 14
#define ENCODER_B 15
#define ENCODER_PORT PINC
#define SWITCH 13
#define BUTTON 12
#define RGB_RED 11
#define RGB_GREEN 10
#define RGB_BLUE 9
#define LED 6
#define SERVO 5
#define PIEZO 3
#define RELAY 2
#define POT 2
#define HALL 3
#define THERMISTOR 4
#define PHOTOCELL 5

void setup() {
   pinMode(BUTTON, INPUT);
   pinMode(LED, OUTPUT);
 }

void loop() {
    if (digitalRead(BUTTON)) {
      digitalWrite(LED, HIGH);
   } else {
      digitalWrite(LED, LOW);
   }
}
									

Getting started

Once the students have entered and settled, you will want to give them a bit of context on what they are dealing with. It is helpful to introduce the arduino as a little computer, but one with a hundred thousand times less power that their laptop. It is much more like the computer that runs their fridge, or car, or the apollo space mission.

What makes it a computer, is that in order for it to function, you need to give it a set of instructions. And that’s what we are going to learn today.

Ask the students to look at their screen. On the projector, Show the sdk. Point out the text area for the instruction set. Point out the compile button at the top left corner of the screen. Get the students to click on it. This will compile the current code, and point out any errors that they may have made.

Now, have the students attach the USB cable to the arduino. Note that the arduino power lights are now lit up. Note that the arduino can draw power either from the USB connection from a computer, or from the power connector just under it.

Have them click on the ‘upload to board’ button at the top corner of the window.

Have the students carefully align and connect the codeshield. Explain that the codeshield is simply a tool that will help them learn the code. Once they start making their own arduino projects, they will likely make their own circuits as well. Have them press the button. The LED will light up.

Introduction to the code

“Now, let’s look at the code.” Direct their attention to the SDK window. Mention that the arduino code references each component by the pin number that it is attached to. Talk about the constants definition, and how they let us use a name instead of a number, making our code easier to write, and easier to read.

Following these are the two main functional blocks of the arduino code. ‘Setup’ and ‘loop’ are called ‘functions’. Functions begin and end with the curly brakcets, and are a way of grouping instructions together. Your arduino code must have both a setup function and a loop function, or the code will not compile.

The setup function is executed once, when the arduiono first gets power.

Once all the instructions in the setup function have been executed, the loop function is called, and then again, and again, until the power is removed.

Making Changes

Now is the time for us to talk about inputs and outputs. Ask students if anyone can explain the difference between an input and an output. After that, explain that an input is a component that sends information to the arduino. An output is a component that takes information from the arduino. Name some components that they are familiar with, and ask the class to tell you wether they are inputs or outputs. Examples are: tv remote(input), tv screen(output), Webcam(input), keyboard(input), computer screen(output), headphones(output), touchscreen(input and output).

In the setup function we need to tell the arduino what pins we are using, and how we are using them. The two lines in the setup function are used to tell the code that the BUTTON pin, pin 12 (point at the function definition), will be used as an input. The LED pin, pin 6, will be used as an output.

The loop function is usually where the interesting things happen in the arduino code. Now, remember what we’ve go going on here. When the button is pressed, the light goes on. Let’s look at the line of code in the loop function. We use digitalRead to ask the processor what the current state of the button pin. Is it on or off? Then, we use digitalWrite to output that value to the LED pin. So when the button is off, the LED is off, when the button is on, the LED is on.

Now that we’ve got the basics out of the way. let’s mess things around. Ask the class to alter the script so that the switch controls the LED. For those that are finished ahead of everyone else, ask them to make the switch light up the blue of the RGB LED. (To do both of these they will need to change the appropriate pinMode line setup, and the line in loop.

So, at this point, we can start to do some interesting things with the arduino. We know everything we need to read the button and switch and to operate the lights and the relay. Using the relay, we can turn on (or off) a lamp, or a radio, or any other low power appliance. The things we’ve learned from the codeshield can also be applied to components on a longer leash. For example, a button on the end of a long wire, mounted next to a door can turn on a radio if the door is open. But, there’s still some things we need to know to get more interesting behaviours out of this thing.

Introducing analog and Serial

Now is the time for us to talk about digital and analog. Ask the students if anyone cares to explain the difference between analog and digital. Explain that digital refers to things that are on or off, true of false, yes or no. A button is either pressed down or it’s not. A switch, as you slide it across is off,, off, off off, ON!. Computers think in digital. in ones and zeros. Humans think in analog. There are all sorts of midpoints between hot and cold, between light and dark, and if we want to get the arduino to sense these, we need to use analog inputs to get away from ones nad zeros.

But first, as we start to get more complex things going on in the arduino, we are going to want a way to monitor what’s going on in that tiny little brain. This is where we can use the computer that uploads the code to also keep tabs on it as it executes.

Have the students add the lines

  Serial.begin (115200);
  Serial.println("Start");
									


to the end of the setup function. These tell the arduino that we are going to want to send information to the computer, and at what speed we are going to do it at. Once that has been uploaded to the board, click on the magnifying glass in the upper right corner of the arduino window. Make sure the pull down in the lower right of the new window is set to a baud rate of 115200, and you should see the word “Start at the top of your winbdow.

Now, although the arduino is behaving the exact same way, this is kind-of exciting, because now, the arduino is communicating with the computer. That little word there, that came from the arduino. That’s exciting! Let’s take it a bit further. Close the serial monitor window, and at the end of the loop function add these two lines:

    Serial.println(analogRead(POT));
    delay(200);
									


Now, the first line here reads the value of the analog pin specified by the constant POT, in this case, analog pin 2, and prints it out to a line in the serial monitor (println is short for print line)

The second line tells the processor to wait for 200 milliseconds. So in effect, we’re telling the arduino to give us new values 5 times a second. Without this line, we’d get new values thousands of times a second, and would overwhelm the arduino SDK on some of your slower computers 🙂

Now, upload to the board, and open the Serial Monitor window. What you’ll see now is a screen full of the same number over and over again. Ask the students to tell you what happens when they turn the taller of the two knobs. They will see new values scroll across their screen as they turn the knob.

Ask the students what the lowest value is that they can get. Then ask for the highest value. They should get a range from 0 to 1023.

Ask if anyone knows what is special about the number 1023. If no one knows, lead them through 1*2, 2*2, 4*2 until you get to 1024. Point out that since the computer thinks in ones and zeros, all the values from 0 to 1023 can be expressed using a series of 10 ones and zeros.

So, now we are successfully reading the analog values from a sensor. The exact same thing can be done for the light sensor, thermistor, and hall effect sensor (magnetic field sensor).

Now, let’s use the value we’ve read to do something. Change the first line of the loop function to

    digitalWrite(LED, analogRead(PHOTOCELL) < 100);

Now we have a night light that will turn on in the dark (cover the light sensor with your finger, and the LED will come on).

Switch Programmers

Now is an excellent time to encourage the kids who are working in pairs to switch duties, so that they both get a chance to type.

Working the Servo

There are many different kinds of motors that can be used. Each has different uses. The motor that we have on the CodeShield is refered to as a servo motor. Servos are used extensively in robotics, as they can be told to turn to a specific angle of rotation. Our servo can be told to go to any angle from 0 to 180 degrees by sending it a pulse of electricity between 1 and 2 milliseconds long.

To do this from the arduino, we could write some code to set the SERVO pin to HIGH, then back to low again after the proper time, but thankfully, there is an easier way. The arduino SDK comes with a number of libraries that we can import that provide easier ways of doing common tasks. To get started, we need to add the following lines at the very top of our file:

#include <Servo.h>
Servo myservo;
									


By adding the first line, all of the code in the servo library becomes available to our sketch, making our job of controlling the servo much much easier. The second line creates an object that we use to control the servo. We need one of these for each servo that we want to control.

We also need to add a line to the setup function that will tell the arduino which pin the servo motor is connected to. Add this to the end of the setup function:

  myservo.attach(SERVO);
									


which tells the arduino that the servo ‘myservo’ is attached to pin 5.

Now we can tell the servo to move to 45º by calling

myservo.write(45);
									


Let’s do something a little more interesting, by reading the current value of the potentiometer, converting it from the 0-1023 range to a 0-180 range, and feeding it into the servo, we can make the position of the servo mirror the position of the potentiometer.

Add this to the loop function before the delay.

    int potval = analogRead(POT);
    int angle = map(potval, 0, 1023, 0, 179);
    myservo.write(angle);
									


You’ll notice that the movement is jumpy. Ask the students if they can understand why (the delay is causing the value to be updated only 5 times a second). Have them reduce the delay to somewhere around 10-30.

Note that what we have here could be used to control a remote camera, or aim a remote nerf gun. Since we also know how to send the value to a computer, we’re not far off from being able to read the potentiometer value on this arduino, send it across the internet to another arduino half the world away, where it can control the servo there.

Extra tasks

If you have extra time remaining at the end of the lesson, here are some extra tasks that you can encourage the kids to try:

  • Flash the RGB LED between red and blue.
  • Read the value of the thermistor or photocell instead of the potentiometer.
  • If photocell moves below a certain threshold, turn on the LED.
  • Use analogWrite(PIEZO, 128); to make a buzzing noise
  • Make the Potentiometer act as a dimmer for the LED (analogWrite(LED, [value from 0 – 255]);

or anything else that you feel might excite the kids.

Wrapping it up

So, we’ve covered a lot of ground, but there’s still a lot we can learn. If you want to recreate any of the codeshield circuits on your own board, the schematics are on the codeshield site at http://diyode.com/codeshield/index.php/CodeShield_Schematics.

Keep an eye out for Lesson 2, where we will look at the electronics underpinnings of what we learned today.

4 comments. Leave a Reply

  1. PY

    Met you at Maker Faire. Bought a DIYode and am enjoying this brilliant piece of fun. Quick comment: Your setup code appears wrong. All the stuff in the setup() should be in the loop() to make a blinky blink. At least that is what I needed to do.

    That is all!

    PY

    • Simon Clark

      Glad to hear you’re enjoying it 🙂 . The purpose of the setup code is to do something to indicate that the code worked, but to be as unsexy as possible, therefore, and single blink, so the teacher knows that that workstation, arduino and codeshield are ready to go. Blinking comes later in the lesson.

      • PY

        “…to be as unsexy as possible, therefore, and single blink, so the teacher knows…are ready to go”

        The master has spoken 😉

Leave a Reply

Your email is never published nor shared.

You may use these HTML tags and attributes:<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>