connex.stao.caconnex.stao.ca/.../files/arduino_teacher_handout.docx · web viewyou will need to...

11
Arduino Teacher Handout About the Arduino The arduino board is connected to the computer with a USB cable. This provides communication and power to the board so it does not need to be plugged in when connected to a computer. Programs for arduino are called sketches. They are a variation of the C programming language. The program for creating and deploying sketches can be found at www.arduino.cc and is open source so it can be downloaded for free as many times as required. (note depending on how much your computers are locked down this may need to be done by board IT staff) When you open the program the first thing you want to do is select the type of Arduino board you are using. Select Toools→ Board→ Arduino UNO (the top one) We are using the UNO board as it is the most common arduino microcontroller.

Upload: hathuan

Post on 14-May-2018

215 views

Category:

Documents


1 download

TRANSCRIPT

Arduino Teacher Handout

About the ArduinoThe arduino board is connected to the computer with a USB cable. This provides communication and power to the board so it does not need to be plugged in when connected to a computer.

Programs for arduino are called sketches. They are a variation of the C programming language. The program for creating and deploying sketches can be found at www.arduino.cc and is open source so it can be downloaded for free as many times as required. (note depending on how much your computers are locked down this may need to be done by board IT staff)

When you open the program the first thing you want to do is select the type of Arduino board you are using. Select Toools→ Board→ Arduino UNO (the top one) We are using the UNO board as it is the most common arduino microcontroller.

The next step is to make sure it is connected to the correct serial port to send information to the arduino. For a Windows computer it is always COM 3 for a Mac it will be something like /dev/tty.usbmodem621

The way the Arduino program connects to the real world is through the pins of your board. The Arduino board has two sets of pins. One is only for analog input and the thirteen at the top are digital input or output. Some of these 3,5,6,9,10,11 can also provide an analog output (only output) using something called pulse width modulation (PWM). However it is important not to use these for analog input as they have lower resistance than the analog input pin and can be damaged by high currents. To use that port you simply plug the red wire or test lead into the port you want to use.

The Arduino output can be either digital or analog. Digital means it is either off ( 0 volts to that pin) or on (5 V to that pin). Analog means the output can be anywhere between 0-5 V. Inputs can also be digital or analog. Again Digital means that it senses a voltage or not and analog reads a voltage between 0-5V.

Programming the Arduino to Output Voltage

Arduino programming is not difficult. Each pwm pin (3,5,6,9,10,11) can be set to output using the command pinMode(pin#.OUTPUT); programmed to output 0-5 V using the command analogWrite(pin#,voltage);

For example the following code can be cut and pasted to allow the student to control the voltage output from pin 5 from 1-5 V using the serial monitor

char volts;

void setup() { pinMode(5,OUTPUT); Serial.begin(9600); Serial.println("Enter a voltage between 1-5");

}

void loop() { if(Serial.available()>0) { volts=Serial.read(); if(volts=='0') { analogWrite(5,0); Serial.println("Voltage set to 0V"); Serial.println("Enter a new Voltage when desired"); } else if(volts=='1') { analogWrite(5,51); Serial.println("Voltage set to 1V"); Serial.println("Enter a new Voltage when desired"); } else if(volts=='2') { analogWrite(5,102); Serial.println("Voltage set to 2V"); Serial.println("Enter a new Voltage when desired"); } else if(volts=='3') {

analogWrite(5,153); Serial.println("Voltage set to 3V"); Serial.println("Enter a new Voltage when desired"); } else if(volts=='4') { analogWrite(5,204); Serial.println("Voltage set to 4V"); Serial.println("Enter a new Voltage when desired"); } else if(volts=='5') { analogWrite(5,255); Serial.println("Voltage set to 5V"); Serial.println("Enter a new Voltage when desired"); } }}

To run the code you Click on the Upload arrow. This builds the code sends the code to the arduino board. If there is an error in the code while building it will not deploy the code.

If you click verify it will build the code to check it but will not deploy it.

Upload

Verify

Once the code is deployed to the arduino it will run it from its own chip and does not need the computer anymore. However to change the voltage you will still need to be connected to use the serial monitor

Using the programmed arduino

Note: Because arduinos are a bit variable you will not get exact voltages. For example the one tested with the code above only gives 3.5 V when set to 5 if connected to a 100Ω resistor but gives 4.8 when connected to a 1KΩ resistor. You will need to know what voltage outputs your circuit gives by measuring with a voltmeter if doing an ohm’s law experiment.

To use the arduino as a Variable power supply you need to use the usb cable to connect it to a computer with the sketches program installed. (If you want to use it as a static power supply you can just use a 9V connector and the 3.3V and 5V pins already on the arduino with no code required)

Students do not need the code if already installed they just need to set the port to the one that appears (usually com 3) and open the serial monitor.

Serial Monitor button

Once in the serial monitor they can enter the voltages using the keyboard and enter key

About Prototype Boards (breadboards)

To connect the power to a breadboard plug the red(positive) wire into the digital port you used in the code (5 in the example) and the black wire into the gnd (ground)

Then plug the red and black wires into the breadboard.

The reason to do this is that the sides of the breadboard are one long connector so you can easily get power from any of the other holes in that line to create multiple parallel circuits.

To make a circuit with the breadboard you need to create a path for electricity from the hot to the ground making sure that power has to flow through the loads you plug in. Open the breadboard would look like this.

For example the first circuit would be in series but the second would not work since the power would flow through the wire on the board not the resistor.

Measuring Voltage

The arduino can also be used to measure a voltage from 0-5 V using its analog input port.An analog input measures the actual voltage between 0-5 V rather than just if it is there or not. For analog input the Arduino measures the voltage into the pin using the command analogRead(pin#); this reads the voltage and returns a digital number between 1 and 1023. The code below uses a potentiometer and records analog voltage.

void setup(){Serial.begin(9600);

}void loop(){

int reading=analogRead(0);float voltage =reading/204.6;

Serial.print(“\t\tVolts=”);Serial.println(voltage);delay(500);

}

There are two things above that might confuse you. First since the analog inputs are only inputs you don’t need to declare what they are in the setup. Secondly the reason for dividing the reading by 204.6 is just math. Since 5V gives a value of 1023 if we divide 1023 by 5 we get 204.6 which is what each volt gives so dividing the reading by that converts the input the computer understands back to the voltage which we understand.

If we run this program as we twist the potentiometer you can see the reading and voltage change.