Вы находитесь на странице: 1из 12

Workshop Arduino English

starters workshop 2
We advice to finish part 1 of this workshop before following this one.
There are a set of assignments in this workshop that can be taken individually. First try assignment 1
and after that you can take them in any order you like.
There is unique material for each assignment so when multiple people are following the workshop
divide them equally and keep the materials together for the next person.
All the programs that are needed for the assignments are available on a USB stick. It will be copies of
this on the desktop of the computers so it will not be necessary to enter this code by hand. It is vital
though to read the code carefully to understand what is happening.

Assignment 1:
The object of this assignment is to known how to use the serial monitor.
There is a serial monitor inside the Arduino IDE, you can use it to write input data from the arduino to
the screen. It can be used to quickly check the state of a push button, temperature sensor or other
device.

Connect the arduino to a USB port on the pc and run this code:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(Hello World!);
delay (100);
}
Now click on the serial monitor and the text Hello Work! will be displayed continuously on the
screen.
Serial.begin(); set the baud-rate of the serial port.
Serial.println(); print the text contents of a variable and continue to the next line.
delay(); make the processor wait for the specified number of milliseconds before continuing with the
program.

In the next example we will read the data from an analogue input port:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(analogRead(0));
delay (100);
}
When we display data from more than one input port it is better to show which port we are reading.
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("analogue port 0 ");
Serial.println(analogRead(0));
Serial.print("analogue port 1 ");
Serial.println(analogRead(1));
delay(100);
}
We now use two new functions:
Serial.print(); print the text version of a variable and stay on the same line.
analogRead(); reads the value of an analogue port. The arduino continues 6 of these ports numbered
from 0 to 5. The value read is 0 to the maximum of 1023 for normally 5 volt.

Assignment 2:
The goal is to learn to show text on a 2x16 LCD display.

There is a tutorial page for LCD screens on the arduino.cc website:


http://arduino.cc/en/Tutorial/LiquidCrystal
Connect the LCD screen to the arduino in the way described on the website.

Use the example code from the arduino page:


// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis()/1000);
}
When this code runs on the arduino turn the pot-meter till the letters on the screens are bright enough to
read easily. The pot-meter will modify the contrast of the display.
This is a nice example to show a static text hello, World! and variable text the number of seconds
since the start-up of the arduino.
Note that the library for the LCD monitor is written in a modular fashion, we can alter which pins the
arduino will use for the LCD display: LiquidCrystal lcd(12, 11, 5, 4, 3, 2); The order of the pins is fixed
to it's function: LiquidCrystal lcd(RS, E, D4, D5, D6, D7);
Now connect to the display to the follow pins (2, 3, 4, 5, 6, 7) and in this order.
Change the arduino code to match these new pins and run the code again.
The ability to change the used pins is needed in the future when you need for example to reserve the
SPI interface (pin 11, 12, 13) for an Ethernet module, SD card or when you want to use as much PWM
pins as possible.
lcd.begin(); the type of the LCD screen in use.
lcd.print(); show a text or a variable on the display.
lcd.setCursor(); move the cursor to the given position. 5, 0 is the 6th token on the first line. 10,1 is
the 11th token on the 2nd line.
Change the code to show your first name in the top right corner and your surname in the bottom
left cornet of the LCD display.

Assignment 3:
The goal is to know how to read a temperature sensor.

This a relative simple variant of a temperature sensor that returns a voltage relative to the power
voltage. Normally a value between 0 and 5 volt.
This is easy to use in combination with the analogue input of the arduino boards.
This project is also fully documented on the internet:
http://www.danielandrade.net/2008/07/05/temperature-sensor-arduino/
Connect the temperature sensor to the arduino as written on the site:

The easiest way to read and show the temperature is via de serial monitor:

Run this code and open the serial monitor:


void setup()
{
Serial.begin(9600); // start serial communication
}
void loop()
{
Serial.println(((analogRead(0)* 5.0 / 1024) - 0.5)* 100);
}
The temperature of the sensor will show in the serial monitor.
Connect the second temperature sensor to the analogue port 1.
Change the code so you can read the 2 sensors.
Make it so that:

both sensors show what sensor data is written.

there is enough time to read the data.

the unit of degrees Celsius is shown after the temperature.


The serial monitor will show something like this:

Assignment 4:
The goal is to work with the data logger shield from Adafruit.

This shield add's 2 extra functions to the Arduino.


1. a real time clock
2. a SD card for data storage
The real time clock:
A real time clock is really handy when writing data to read it at a future time.
You can read precisely when something is logged.
When you use the clock for the first time or when the battery is replaced you need to set it to the
current time.
In this program we set the clock to the clock on the pc at the time that the code is uploaded:
RTC.adjust(DateTime(__DATE__,__TIME_));
On the website of Adafruit is all the information needed to use this shield:
http://www.ladyada.net/make/logshield/lighttemp.html

We will now show the data on the serial monitor that the clock sends:

#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;
void setup () {
Serial.begin(9600);
Wire.begin();
RTC.begin();
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
RTC.adjust(DateTime(__DATE__,__TIME__));
}
}
void loop () {
DateTime now = RTC.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
delay(1000);
}
Upload this code to the arduino with the data logger shield attached to it and start the serial monitor.

The console should now look like:

We will now continue with the SD card:


We can use the SD card to store data and to read it back again.
For this assignment we use the temperature sensor like in assignment 3.
Connect the sensor to the arduino like in assignment 3.

We will store the data we read with the temperature sensor on the SD card. We will log the time
together with the data so we can read what the temperature was on a given moment.
The code to do this is a bit too large to type into the arduino environment. It is on the USB stick or on
the desktop (opdracht 4_2). It is directly copied from the Adafruit website.
In the serial monitor you can read what data is written and to which file on the SD it is written.
Remove the USB connector from the laptop.
Take the SD card out of the shield and put it directly into the laptop or into an available SD card reader.
There will be a file on the SD card with the file-name that where shown in the serial monitor. Open it
with the laptop.
On the Adafruit website it is shown how to read this data with for example Excel.
This is outside the scope of this workshop but when you are interested open:
http://www.ladyada.net/make/logshield/lighttemp.html

This was the beginner arduino workshop version 2.


You now have enough information to start your own projects.
When there is extra time you can try to combine the material of several assignments.
For example you can show the temperature and time on a LCD display.
In TkkrLab we have a wide variety of hardware to create more sophisticated projects.
We also have the possibility to move you're projects from a prototype to a normal electronic print
board.
So come by any time to look what the possibilities are.
www.tkkrlab.nl

Вам также может понравиться