loading

The following information is a single lesson in a larger project. Find more great projects here.

Return to Previous Lesson: Introduction to Arduino

Lesson Overview:

Now we'll learn how to read sensors and print to the computer to see how the program is running!

Step 1: Getting Started

An Arduino can be made to observe the world around it using its analog inputs.

These are special inputs that allow the Arduino to measure the voltage connected to them.

By connecting analog sensors to the analog input pins of an Arduino, it can be used to observe the world around it and measure things like the amount of light and the temperature.

In this tutorial you are going to learn how to take measurements of things happening in the real world using an Arduino, and to communicate with your computer.

You will use these skills to make a simple weather station, like the picture below. It tells you what kind of weather is going on outside. You will learn how to read sensors and how to send messages from the Arduino to your computer.

  1. Continue to the next step.

Step 2: Computer Talk

When you program the Arduino and run simulations, it’s sometimes difficult to know exactly what's going on inside the Arduino. So it can be difficult to check if the observed behavior is really what you wanted it to do.

But when you connect a real Arduino to your computer using an USB-cable, the Arduino can talk to your computer and vice versa. It can send messages about what it’s doing at the moment. This can be very useful to check your program and solve problems with your code (called ‘debugging’).

  1. Continue to the next step.

Step 3: Serial Monitor

There are many ways and languages for electronics to talk to each other. The Arduino talks to your computer, using ‘Serial Communication’.

The 123D Circuits editor, where you can make and simulate your Arduino projects, is able to simulate this communication as well.

There is a serial monitor where messages from the Arduino are shown and where you can send messages back to the Arduino.

  1. Open the code editor.
  2. Click on the Serial Monitor (upper right button in the code editor).
  3. Continue to the next step.

Step 4: Sending Messages

You will now learn how to instruct the Arduino to send messages.

To be able to send messages, the Arduino needs to open a new communication channel. It is like pressing the call button on a phone: from that moment you open a communication line to call.

You can do this by adding the following instruction to the 'setup' function of your Arduino code:

Serial.begin(9600);

The argument to Serial.begin tells the Arduino how fast to communicate. Here you are telling it to send 9600 bits per second (baud).

To send a message, add the following instruction to the end of 'setup':

Serial.println(“Hello world!”);

  1. Add the 2 instructions above to the setup function.
  2. Start the simulation without closing the code editor.
  3. Observe the result in the serial monitor.
  4. Continue to the next step.

Step 5: Sensors

Now that you can communicate information, the next thing to do to create the weather station is to add sensors to observe the weather.

You will add a photodiode to measure the ambient light, and a thermistor to observe the temperature.

  1. Continue to the next step.

Step 6: Light Sensor

A photodiode converts light into electrical current in the reverse direction.

Based on the amount of light we can determine whether the sun is shining, it’s partly clouded or maybe it’s dark thunder-like weather.

  1. Click "Components", search for the photodiode, and add it to the breadboard.
  2. Connect the negative terminal (cathode) to the red row at the top of the breadboard (+5V).
  3. Add a resistor to the breadboard and set its value to 47 kΩ
  4. Connect one side of the resistor to the blue row on the top of the breadboard (GND).
  5. Connect the other end of the resistor to the anode of the photodiode and to analog pin A0 of the Arduino.
  6. Continue to the next step.

Step 7: Temperature Sensor

A thermistor is a resistor whose resistance varies with temperature. A thermistor is to temperature what a photoresistor is to to light, or a potentiometer is to position.

No thermistor component exists in the simulator, so to simulate this, you will use a photoresistor instead.

  1. Search for the photoresistor and add it to the breadboard.
  2. Connect the left terminal (1) to the red row at the top of the breadboard (+5V).
  3. Add a resistor and set its value to 470 Ω
  4. Connect one side of the resistor to the blue row on the top of the breadboard (GND).
  5. Connect the other side to the right terminal of the photoresistor. and to pin A1of the Arduino.
  6. Continue to the next step.

Step 8: Reading the Sensors

Now it is time to try reading the sensor values.

To do this, you will use the Arduino function ‘analogRead’. This function reads the analog voltage on the specified pin and returns a value between 0 (0V) and 1023 (+5V).

sensorLight = analogRead(A0);
sensorTemp = analogRead(A1);

To check what value the Arduino has read you can use the 'Serial.print' and 'Serial.println' functions. 'Serial.print' is the same as 'Serial.println', but doesn't start a new line at the end.

  1. Change the code in the loop function to be:
    Serial.print("A0 = ");
    Serial.print(analogRead(A0));
    Serial.print(", A1 = ");
    Serial.println(analogRead(A1));
    delay(1000);
  2. Start the simulation with the code editor active.
  3. Observe what happens when the slider on the photodiode and thermistor (photoresistor) are adjusted.
  4. Stop the simulation.
  5. Continue to the next step.

Step 9: Interpreting the Values

You may have observed that the light sensor gives values in the range 46 to 964, and the temperature sensor range is 3 to 492.

It would be more meaningful to convert these values to 0 to 100 percent for light, and Celcius for temperature.

Suppose that through actual temperature measurements, you determine that the temperature conversion from these values to degrees Celsius is:

Celcius = analogRead(A1) ✕ 0.14 - 23

Change the program to report the more meaningful values.

  1. Change the code in the 'loop' function to be: int light; int celsius;
    light = (analogRead(A0) - 46) / 9.1;
    celsius = analogRead(A1) * 0.14 - 23;
    Serial.print("Light = ");
    Serial.print(light);
    Serial.print(", Temp = ");
    Serial.print(celsius);
    Serial.println(" C");
    delay(1000);
  2. Start the simulation and observe what happens to the values.
  3. Stop the simulation.
  4. Continue to the next step.

Step 10: Interpreting the Results

Perhaps more readily understandable results could be displayed.

Change the display according to the following.

For light:

0 to 20 = Stormy

21 to 40 = Cloudy

41 to 60 = Partly cloudy

61 to 80 = Partly sunny

81 to 100 = Sunny

For temperature:

Below 5 = Cold

5 to 16 = Cool

17 to 26 = Mild

27 to 37 = Warm

Above 37 = Hot

  1. Add the following code before the 'delay' in the 'loop' function:
    if (light < 21) Serial.print("Stormy");
    else if (light < 41) Serial.print("Cloudy");
    else if (light < 61) Serial.print("Partly cloudy");
    else if (light < 81) Serial.print("Partly sunny");
    else Serial.print("Sunny");
    Serial.print(" and ");
    if (celsius < 5) Serial.println("cold");
    else if (celsius < 17) Serial.println("cool");
    else if (celsius < 27) Serial.println("mild");
    else if (celsius < 38) Serial.println("warm");
    else Serial.println("hot");
  2. Start the simulation and observe what happens.
  3. Stop the simulation.
  4. Continue to the next step.

Step 11: Finishing Up

You now have a working weather station!

This could be expanded by connecting other sensors to measure things like wind speed, air pressure and humidity. It could also be expanded to include LEDs to give immediate visual information about the current temperature and light.

  1. Well done!

In the next lesson you will learn to create analog signals!

Next Lesson:Using Analog Output

About This Instructable

296views

7favorites

License:

More by circuits:Introduction Arduino Project 15: Hacking Buttons Arduino Project 14: Tweak the Arduino Logo 
Add instructable to: