Category Archives: arduino

Arduino Workshop for Teachers @ EMU

Presented by SIGCS and The Department of Computer Science at Eastern Michigan University

This workshop is intended for teachers who are interested in learning to wire and program the Arduino. Arduinos will be supplied for use during the workshop. We are using kits purchased from Amazon and Parallax – Arduino robots.

Introduction:

I’ve been teaching a one semester class with Arduino for ten year. I learned how to use Arduino along with my students and my course has continuously evolved over time. My complete course is posted on this website.

From the Arduino website:

Arduino is an open-source electronics platform based on easy-to-use hardware and software. Arduino boards are able to read inputs – light on a sensor, a finger on a button, or a Twitter message – and turn it into an output – activating a motor, turning on an LED, publishing something online. You can tell your board what to do by sending a set of instructions to the microcontroller on the board. To do so you use the Arduino programming language (based on Wiring), and the Arduino Software (IDE), based on Processing.

Blink:
Lets make sure the boards will communicate and take a quick look over the Arduino IDE. We are going to cause an LED on the Arduino to flash at different rates.

Paste this Code into the Arduino IDE:
Here’s the complete code:

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

If you have programming experience this should look very familiar. If you are new to programming, this page will explain all the code. Play a bit, make the LED flash at different rates.

Now build the circuit shown below (need more help?). If you’ve done it correctly the LED on the Breadboard will flash along with the LED on the Arduino board.

Breadboard with Arduino and LED

Add on a couple more LEDs (don’t forget the resistors). Then make all the LEDs flash together and/or in sequence.

Digital Input:

Next we’ll add in a push button. Build this circuit:

Arduino wired to LEDs on Pins ( and 10 and a pushbutton on pin2

Try this Code. You won’t see anything unless you open the Serial Monitor:

int buttonPin = 2;
int buttonState = 0;
int led = 10;

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(led, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  delay(10);
  buttonState = digitalRead(buttonPin);
 
  if (buttonState == HIGH)
  {
    digitalWrite(led, HIGH);
    Serial.println("Button Pressed");
  }
  else
  {
    digitalWrite(led, LOW);
    Serial.println("Button Released");
  }
}

Now, do something with the second LED. Doesn’t really matter what. You should also check out the Serial Monitor. You should notice that “Button Pressed” and “Button Released” print over and over again. This code will stop that from happening:

int buttonPin = 2;
int buttonState = 0;
int buttonPressed = 0;

void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
}

void loop() {
  delay(100);
  buttonState = digitalRead(buttonPin);
  
  if (buttonState == HIGH && buttonPressed == 0)
  {
    Serial.println("Button Pressed");
    buttonPressed = 1;
  }
  
  if (buttonState == LOW && buttonPressed == 1)
  {
    Serial.println("Button Released");
    buttonPressed = 0;
  }
}

This should print Button Pressed only one time when the button is pressed. This idea can be built upon to create a toggle. The code below will turn an LED on when the button is pressed and the LED will stay on until the button is pressed again.

HIGH, true, and 1 are Synonymous:

int buttonPin = 2;
int buttonState = 0;
int buttonPressed = 0;
int ledPin = 10;
bool ledState = false;

void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  delay(100);
  buttonState = digitalRead(buttonPin);
  
  if (buttonState && !buttonPressed)
  {
    Serial.println("Button Pressed");
    buttonPressed = 1;
    ledState = !ledState;
  }
  
  if (!buttonState && buttonPressed)
  {
    Serial.println("Button Released");
    buttonPressed = 0;
  }

  digitalWrite(ledPin, ledState);  
}

Analog Read and Write:

Time to explore the Example Sketches. In the Arduino IDE open:

File->Examples->Analog->Fading

Wire an LED (with resistor) up to Pin 9 and upload the code. You should see your LED pulsing on and off. Can you make it faster? Slower?

Things to know:

    • analogWrite is 8 bit (0-255)
    • overflow – 256 is the same as 0
    • Only certain pins support analogWrite. Marked with either PWM or a ~

Next load up:

File->Examples->Analog->AnalogInput

Potentiometer wired to pin A0 LED wired to pin 10

Change the cod to light up the LED plugged into pin 10. The turn the knob to see the flash rate change. Then try this code:

int sensorPin = A0;    
int ledPin = 10;
int sensorValue = 0;
int val = 0;

void setup() {
  pinMode(ledPin, OUTPUT);

}

void loop() {
  sensorValue = analogRead(sensorPin);
  val = map(sensorValue, 0, 1023, 0, 255);
  analogWrite(ledPin, val);
}

analogRead returns a 10 bit number (0 – 1023), so we use the map() function to adjust it to use with analogWrite. If you are feeling ambitious you can swap your potentiometer out for a light sensitive resistor (Note: the code example on this page is incomplete. You will need to fix it).

Make things Move
Next dig out the Servo motor and wire it as shown below:

Servo wired to pin 9 and potentiometer wired to A0

Open: File->Examples->Servo->Knob

Note: Standard servo motors adjust from 0 to 180°. Here we use map() to adjust our analogRead values to this range.

Arduino Robot

We are using the NICERC Shield Bot with Arduino Kit. The kit comes with a complete guide, but I don’t know that I’d use it as more than a general reference. I should note that I am not a classically trained coder. So I might be wrong in this.

Here are a couple of my general recommendations:

  • Do not use Pins 12 and 13 for the servos as recommended. When an Arduino is powered up or reset it hits pin 13 briefly. If you use Pin 13 for one of your servos the robot will move slightly when this happens. Instead use the other set of headers, which are wired to pins 10 & 11.
  • You might want to use servo.write() instead of servo.writeMicroseconds(). Most of the example Arduino servo code you (or your students) will find on the net uses servo.write(). Additionally if you add a standard servo to the robot later you will probably use this function as well.

I will be using servo.write() in this workshop. The servo motors on our robots have been modified to be continuous rotation servos. Wire your potentiometer (from the Arduino Kit) to your robot as shown:

Use the Servo example sketch we used earlier: File->Examples->Servo->Knob

Change the servo pin to one of the pins attached to our robot’s servos. Note how the wheel responds as you adjust the servo. For continuous rotation servos, 90° should cause the servo to stop, 0° is maximum speed in one direction and 180° is max speed in the other direction.

Be sure the robot’s servo motors are attached to pins 10 & 11 (or change the code) and try this:

#include <Servo.h>

Servo left;  // Creates an instance of Servo for the left motor
Servo right; // Creates an instance of Servo for the left motor

void setup() {
  right.attach(10); // attach right servo to pin 10
  left.attach(11);  // attach left servo to pin 11

  forward(1000);
  turnRight(900);
  forward(1000);
  turnLeft(900);
  stop();
}

void loop() {

}

void stop(){
  right.write(90);  // setting servos to 90 degrees
  left.write(90);   // stops them from turning
}

void forward(int driveTime){
  // Both just as far from 90 degrees means both motors
  // should be turning the same speed. Opposite directions
  // becuase motors are oriented in different directions.
  right.write(60);
  left.write(120);
  delay(driveTime);
}

void backwards(int driveTime){
  right.write(120);
  left.write(60);
  delay(driveTime);
}

void turnRight(int turnTime){
  right.write(90);
  left.write(120);
  delay(turnTime);
}

void turnLeft(int turnTime){
  right.write(60);
  left.write(90);
  delay(turnTime);
}

void disableServos(){  
  right.detach(); // disconnect servos
  left.detach();
}

Challenge: Write code to navigate the course taped out on the floor.

Add simple sensors to your robot:
Flip to page 148 in the guide and add the Whiskers to your robot.

The Whiskers act as push buttons. In their current configuration they will read as HIGH when the whisker is not touching anything. When the whisker is pushed back into the headers, this will “close” the button and the digitalRead() will return LOW.

Here’s some quick code I put together to read the whiskers:

#include <Servo.h>

Servo left;  // Creates an instance of Servo for the left motor
Servo right; // Creates an instance of Servo for the left motor

bool rightWhisker;
bool leftWhisker;

void setup() {
  pinMode(7, INPUT);
  pinMode(5, INPUT);

  
  right.attach(10);
  left.attach(11);

}

void loop() {
  
  forward(10);
  
  rightWhisker = digitalRead(7);
  leftWhisker = digitalRead(5);
  
  if(rightWhisker == LOW){
    backwards(500);
    turnLeft(900);
  }else if(leftWhisker == LOW){
    backwards(500);
    turnRight(900);
  }
}

void halt(){
  right.write(90);  // setting servos to 90 degrees
  left.write(90);   // stops them from turning
}

void forward(int driveTime){
  right.write(60);
  left.write(120);
  delay(driveTime);
}

void backwards(int driveTime){
  right.write(120);
  left.write(60);
  delay(driveTime);
}

void turnRight(int turnTime){
  right.write(90);
  left.write(120);
  delay(turnTime);
}

void turnLeft(int turnTime){
  right.write(60);
  left.write(90);
  delay(turnTime);
}

It could probably use some additional tweaking and maybe an extra function or two to make it work better. This one will get stuck in a corner pretty easily. Can you change the code to keep that from happening?

Where Steve Buys stuff:

Cheap sensor help students answer real questions

I gave a presentation a couple months ago at the Spring meeting of the Michigan section of the American Association of Physics Teachers highlighting a project a pair of my electronics students did last school year. My students used an Arduino to read a 250g accelerometer to investigate the force a brain might feel in a violent football tackle.

From an Arduino point of view it was a trivial program. However, it was still a cool project for a variety of reasons. There were many opportunities for problem solving. They had to figure out how to embed the sensor in a meangingfull way, mount the helmet, and simulate a rough tackle. First task was determining how to mount the sensor. They asked if they could 3D print a head. This seemed reasonable to me, but I wasn’t sure if they’d have to design it or if we could find one. The head of Stephen Colbert was readily available and made us laugh, so that’s the one we printed after modifying it to accommodate the sensor. In retrospect this was not the best head to print as Colbert’s hair when 3D printed doesn’t squish the way real hair would. For this project it worked out fine, but for a side impact would not be ideal.

3D Printed Stephen Colbert in Helmet
I really like this project because it gave students a chance to investigate something of interest to them that is also very topical. As football players, this was of direct interest to them and something with wider potential impact as well. When they finished it I immediately wanted to share this project with other physics teachers. It would be cool to see other teachers working with their own students to do similar projects. However, whenever I try to show teachers how to use Arduinos to collect data, their eyes start glaze over as soon as the code hits the screen.

I decided to attempt to meet my physics colleagues where they are rather than where I am. Most of the physics teachers I know have access to either Vernier or Pasco interfaces and sensors. At our school we have Vernier, so that’s what I used. I assume you could do something very similar with Pasco equipment.  Vernier sells a cable you can use to make your own analog probeware. It turns out this was very easy to attach to our $30 accelerometer.

img_0322.jpg

The Black Wire goes to GND, the Orange Wire is +5V so goes to the VCC, and the Red Wire attaches to OUT. The other wires were not used. All you need to do is solder these three wires to the sensor and plug it into a LabQuest or LabPro. This is something pretty much anybody can do. However, if you’ve never soldered before I recommend this tutorial from SparkFun Electronics.

The 250g Accelerometer we used is an analog sensor. This makes it easy to interface with Vernier hardware. Nerd Alert: If you need to know, basically we are using it as a voltage comparator. On the LabQuest (or LabPro) we set up our sensor to read Raw Voltage (0 – 5V). For our sensor, zero volts corresponds to -250g’s, five volts with 250g’s, and at 2.5 V we have zero g’s. In reality the 5 V wire gave me 5.2 V (the USB standard is 5 V but can be up to 5.25 V or as low as 4.4 V), so zero g’s was at 2.6 V and 250 g’s would be 5.2 V. Since the output from this sensor is linear, I used the LoggerPro program to convert the voltage readings to g’s by creating a “New Calculated Column”. I ended up with a slope of (500 g’s)/(5.2 V) and a y-intercept of -250 g’s.

Screen Shot 2016-07-28 at 8.36.54 PM

The graph of my calculated column resulted in a graph of force vs. time. In the example graph below, the hit lasted for about 0.003 s and reached a peak of just over 63 g’s. Based on readings from the literature, a hit of this magnitude and duration would be unlikely to cause a concussion.

Football graph

With the growth of the Maker Movement there are now a lot of cheap sensors out there that can be interfaced in exactly the same way. Adafruit makes a 200g 3-axis accelerometer that looks promising, but you’d need 3 Vernier cables to read all thee axises simultaneously (also true with Vernier’s 3-Axis Accelerometer).  I’ve also been thinking about using some flexiforce pressure sensors to measure the force/area actually applied to the head in a collision. This would be a simple modification of this lesson on the Vernier site.

Arduino RC Car Part 2

Tony DiLaura asked me about tutorials for high school students to build Arduino controlled RC cars. I didn’t really know of any that I liked so I started outlining what one might look like. This is part two, you might want to check out part one first. This post will be about control mechanisms.

In my last post I said my students had never made remote controlled cars. It turns out this was not at all correct. I’m getting too old and the memory begins to fade. It’s a good thing the internet never forgets. In this post I’ll share some that my students have made in the past. In the next post I’ll dive into ones I’ve seen on the net but haven’t tried yet.

Wiichuck

WiiChuck carOK, this one isn’t radio controlled, but wired instead. It uses the Wii Nunchuck to control your circuit. I suppose if you have a wireless nunchuck you could also make this wireless. If you have to buy a nunchuck this might not be the cheapest option, but I bet one of your students has one at home in a closet somewhere. You can chop the end of the Wiimote plug or you can buy a cheap adapter

  • SparkFun $1.56 – Cheapest and I’ve used this one in the past
  • Adafruit $3.00 – Might be a better option. Looks like it’s designed to snap in and stay in a bit better.
  • WiiChuck Page – All about using a WiiChuck with Arduino including a library download
  • Lego – Lego Electronic GizmO – Lego robot controlled by Arduino and Wiichuck created by my students several years ago. Includes some sample code. Uses the joystick because the accelerometer was a bit too twitchy for them. That could have been solved with programming.

TV Remote

zwally-IR RobotWe’ve also had luck controlling Arduino robots with old TV or VCR remotes. Be aware that some work far better for this than others. The only way to know is to experiment. This is really in the same price range as the Wiichuck solution and makes your car wireless as long as you have line of sight. In theory, you could also create your own remote using a second Arduino and IR LED, but I’ve never done this.

Keyfob

rf receiverI’ve also had students use a keyfob transmitter and receiver. This uses RF and is probably the simplest from a programming standpoint. If your students can program an Arduino to respond to a button press then they know everything they need to to make this work. I don’t have any links to the modules my students used. I can’t remember where I bought them from. Which is probably just as well, they worked, but were not great. However, I did find virtually the same thing on Adafruit’s website. I trust Adafruit to have good stuff.

 

Tutorial for an Arduino RC Car?

A buddy of mine recently sent me a tweet:

RC Arduino Tweet

This is an interesting question the short answer I have is, “No.” The longer answer is a bit more nuanced, so I asked for a little clarification. This is what I got back:

RC Arduino 2

Now I had a lot more to go on. With this in mind I have something to sink my teeth into. Now we have something, so now my response is, “No, I haven’t seen anything that would fit.”

OK, now that that’s out of the way lets cover how I would tackle this problem. Tony is an awesome math teacher in Zeeland, Michigan. He co-teaches a project based math/physics class. I’m going to work from the assumption that he’s working with students starting with little previous knowledge of electronics and Arduino programming, and that he’d like to leave as much room as possible for students to explore. Everything I cover will be with that in mind.

With a project like this you need break it up into pieces. I’m going to think of this as a robot even though it won’t be autonomous because we still will have a computer controlling an independently moving device. As with any robot type project you have three fundamental challenges, the programming, the electronics, and the mechanics. However, with this project we’ll need to consider a forth part I don’t usually think about with a robot, and that is the control mechanism.

Mechanics

You could go crazy with this and start with a platform like Tuggy from the very cool OpenRC Project. While totally awesome I think this takes all of the thinking away from the students and simply turns them into mechanics. Which is fine if that’s your goal. Instead I’d start with ThinkFun’s MakerStudio collection of building sets.

macaroni box car

You can buy sets or download and print from the Thingiverse. I’d start by having students play with the gears and such and make simple cars with parts available, then begin thinking about what their RC car needs. They’ll need to make a variety of decisions. How will their cars be steered? Will they use skid steering (like a tank) or rack and pinion (like a car)? What sort of  platform will they need? I’m not sure a Mac & Cheese box is the best choice. How many motors will they need? Will they use gearbox motors or simple DC motors and then use the gears in the set? Some of these decisions might be made by the teacher and some or all might be left out the students. I’d probably go with simple DC motors and use the gears from the set.

Once these decisions have been made students can then think about the parts in the MakerStudio kits they don’t have but need. Things like motor mounts, rack and pinion mechanisms, bits to mount the gears to the platform of choice, and such. These could should all be designed and 3D printed by the students.

Electronics

Tony asked for Arduino, so we’ll stick with that. It also doesn’t hurt that I know a lot about using Arduinos with high school students. Unfortunately, you can’t run any sort of reasonable motor directly from an Arduino. You need some sort of transistor or h-bridge. Digital outputs on your Arduino only put out 40 mA, this is woefully inadequate to power a motor.


Students can wire an h-bridge themselves, but I highly recommend using a motor shield. I’ve had many students use an h-bridge and breadboard their circuit which mostly works. There are a lot of connections that need to be made and by the time students got to soldering stuff together numerous problems would typically crop up. In order to get around a lot of headaches I now have students use motor shields and skip all the complex wiring. You can buy shields from China really cheaply, but I like to use SparkFun Electronics. SparkFun is based out of Colorado and they offer an educator discount of 20% and allow you to easily set up payment accounts allowing you to pay via purchase order. The other thing I really like about SparkFun is that they include code example and/or tutorials for almost everything they sell. So I can hand a shield to a student and then point them to the product page and step back.

Most motor shields will allow you to control two motors, perfect for skid steering. If your students chose to go with rack and pinion then you can get away with one drive motor, but you’ll need a servo-motor for steering. There are lots of tutorials for controlling servos with Arduino and ideally where ever you get your motor shield will tell you how to wire your motors to it and give you a simple program to control your motor(s).

Other Stuff

At this point we just have programming and control mechanism left. I have some ideas about control mechanisms, but I haven’t done any of these in the past. When I say “I” that really means my students. So, I’m going to do a little more research and then another post. More than likely I’ll be looking at some sort of the cool BlueTooth module and an app running on a cellphone or tablet. There are other ways, but controlling a robot with your phone is just too cool.

Arduino in High School Electronics

I started using Arduino, an open source electronics platform in my one semester electronics course. Arduino incorporates a microcontroller (mini computer) and allows for a lot of really cool options. Last semester was the first semester I used Arduino with the whole class. It went fairly well.

All students were required to create a final project. I gave them the option of working with a partner with the caveat that when they presented their final project I would get to decided which of the two of them would have to answer my questions.

Their projects included not only the circuitry and programming, but students also had to physically build the device. This ended up being a really good project. Even if they chose to build upon a project they found on the net they had to tinker with it to make it do what they wanted it to do.

Once completed, students had to write up and present their projects on Google Sites. If you’re interested you can go to the class site to find descriptions, pictures and Arduino code. Below is a video created by a pair of my students who used an old remote controlled car as the chassis for an autonomous Arduino controlled robot.


Crossposted from FlosScience

Arduino – eeePC Nightmare!!!

So, I decided to totally change the way I teach my electronics class. It will now center around the Arduino microcontroller, a platform that uses an Atmel ATmega 64/128 microcontorller. Anyway, I don’t really have enough laptops to go around, so I scrounged some funds together and bought two of the Asus eeePC 2G Surfs. I figured these would be just about perfect.

Problem #1: Java, which is needed for Arduino doesn’t come on the 2G Surf. So, after searching for a way to get it on the 2G Surf for a few hours I find it. It installs and all is good.

Problem #2: To really make the Arduino sing I also need Processing. Unfortunately there’s not enough room left to fit it.

Problem #3: After a bit more searching I find a way to delete stuff I’m never going to need (like Chinese language support) from the eeePC. Unfortunately this makes no space available to me.

Apparently there are two main partitions on the eeePC. One for the user to do whatever they want and one that the user really can’t touch. All the programs come installed in the one the user really can’t touch.

Solution? After maybe 10 hours yesterday and another 7 hours today I think I may have this licked. I’ll write up process and post it to FLOSScience for those interested. If I’m right I should be able to capture am image of my one working one and put it on the other one. This would take less than 5 minutes, otherwise I’ll be looking at at least another 2-3 hours to get the other one set.

Better Solution? Get the 4G version ($50 more).

Arduino or PICAXE?

This is a pretty geeky post… You’ve been warned.

Among the courses I teach is electronics. It is a one semester course, where we cover basic theory in the first half of the semester and students breadboard circuits for the second half of the semester. I inherited the course and its general structure from the previous electronics teacher and it seems to work pretty well.

Anyway, I’ve been thinking about changing it. I’ve been wanting to incorporate microprocessors into the course for some time, but I’ve been daunted with either the price tag or for the cheap stuff with the technical know-how that I just don’t have. By bringing in microprocessors I can also get students doing some programming . Additionally, a lot of electronics these days includes embedded microprocessors.

Enter the PICAXE and Arduino micro-controllers. Both can be had relatively cheaply and are fairly easy to program. Unfortunately I discovered these at about the same time. If I only knew about one of them I’d have been content to buy a bunch and get to work redesigning my course. As it stands right now, I have no idea which one to choose. So I’m writing this post to get my ideas down and I’m partly hoping for some insight from potential experts.

PICAXE: The PICAXE micro-controller is a Pic based chip that has a bootloader loaded onto it allowing for a very simply programming cable to be used. It can be programmed using a form of basic or with a flow chart based interface. There are some great guides online for its use, but they’d have to be edited a bit to make them usable for my course. There are multiple versions of this chip with different numbers of inputs/outputs. The programming is slightly different from chip to chip.

Pros: Cost. The chip (8M) itself costs less than $4. Theoretically students could easily make permanent projects with this. Basic programming: I know basic and basic programming is relatively easy.

Cons: The 8M, the chip I’d focus on, is a bit limited. There are other chips we could add in, but their structure is different. Apparently limited availability. I’d really have to plan ahead to ensure I had the supplies I’d need (unless I want to order from the UK). Windows only.

Arduino: This board is really a Basic Stamp replacement. I’d thought it was going to be outside of the price range I’d set for the course, but then I found a supplier who supplies a version that I can get for $11/board in bulk. The programming for this chip is in a C++ like language. There is also a lot online support for this board and at least one project designing a course in basic electronics with the Arduino as the platform.

Pros: Expandability. This chip has some wickedly cool potential. It interfaces circuitry directly with a computer easily, making it possible to create circuits that control or respond to functions on the computer. Support for Mac, WinXP, and Linux.

Cons: I don’t know C++ or any other object oriented programming language. More expensive so it would cost more for students to make permanent projects.

I guess the bottom line is the PICAXE is probably simpler to start using but while the Arduino has a steeper learning curve at the start it allows for more advanced functions more easily. At least I think. Right now I’m leaning towards the Arduino. I’ve already got one independant student working with the PICAXE. I’ll get a couple working with the Arduino this semester as well and see what they all think.