Introduction: Arduino Datalogger

In this tutorial, we're going to make a simple data logger using Arduino. The point is to learn the very basics of using Arduino to capture information and print to the terminal. We can use this basic setup to complete a range of tasks.

To get started:

You will need a Tinkercad (www.tinkercad.com) account. Head over and sign up with your email or social media account.

Logging in takes you to the Tinkercad Dashboard. Click "Circuits" to the left and select "Create new Circuit". Let's get started!

You can find the complete file on TInkercad Circuits - Thanks for checking it out!

Step 1: Add Some Components

You'll need some basic components. These include:

  • Arduino board
  • Breadboard

Add those by searching for them and click-dragging them to the middle area.

Place the breadboard over the Arduino. It makes it easier to view the connections later.

Step 2: A Note About Breadboards

A breadboard is a super helpful device for rapid prototyping. We use it to connect components. Some things to note.

  1. The dots are connected vertically, but the line in the middle separates this connection from the top and bottom columns.
  2. Columns are not connected left to right, as in across the row. This means that all components should be connected across the columns rather than down them vertically.
  3. If you need to use buttons or switches, connect them across the break in the middle. We'll visit this in a later tutorial.

Step 3: Add Two Sensors

The two sensors we are using are a Photosensitive sensor and Temperature sensor.

These sensors evaluate light and temperature. We use Arduino to read the value and display it in the Serial monitor on the Arduino.

Search for and add the two sensors. Make sure they are positioned across the columns on the breadboard. Put enough space between them to make it easier to see them.

Step 4: Photosensitive Sensor

  1. For the photosensitive sensor, add a wire from the 5V pin on the Arduino to the same column as the right leg on the part in the breadboard. Change the wire colour to red.
  2. Connect the left leg via the pin in the same column to the A0 (A-zero) pin on the Arduino. This is the analog pin, which we will use to read the value from the sensor. Colour this wire yellow or something other than red or black.
  3. Place a resistor (search and click-drag) on the board. This completes the circuit and protects the sensor and pin.
    • Turn it around so it goes across the columns.
    • Connect one leg to the right leg column on the breadboard
    • Place a wire from the other end of the resistor to the ground
      • Change the wire colour to black.
  4. Double check all connections. If something isn't in the right place, this won't function correctly.

Step 5: Start the Code

Let's look at the code for this component.

First, look at the third image in this step. It contains some code with two functions:

void setup()

void loop()

In C++, all functions provide their return type, then the name, then the two round braces that can be used to pass in arguments, usually as variables. In this case, the return type is void, or nothing. The name is setup and the function takes no arguments.

The setup function runs once when Arduino boots (when you plug it in or attach batteries).

The loop function runs in a constant loop from the millisecond the setup function completes.

Everything you put in the loop function will run when Arduino runs. Everything outside that will only run when called. Like if we defined and called another function outside the loop.

Task

Open the Code panel with the button in Tinkercad. Change the Blocks dropdown to Text. Agree to the warning box that pops up. Now, delete everything you see except the text in the third image in this step.

Variables

To get started, we need to assign some variables so we make our code really efficient.

Variables are like buckets that can only hold one object (C++ is what we call object-orientated). Yes, we have arrays, but these are special variables and we'll talk about them later. When we assign a variable, we need to tell it what type it is, then give it a value. It looks like this:

int someVar = A0;

So, we assigned a variable and gave it type int. An int is an integer or a whole number.

"But you didn't use a whole number!", I hear you say. That's true.

Arduino does something special for us so we can use A0 as an integer, because in another file it defines A0 as an integer, so we can use the A0 constant to refer to this integer without having to know what it is. If we just typed 0, we would refer to the digital pin at position 0, which wouldn't work.

So, for our code we will write a variable for the sensor we have attached. Whilst I recommend giving it a simple name, that's up to you.

Your code should look like this:

int lightSensor = A0;
void setup() {
}

void loop() {
}

Now, let's tell Arduino how to handle the sensor on that pin. We'll run a function inside setup to set the pin mode and tell Arduino where to look for it.

int lightSensor = A0;
void setup() {
	pinMode(lightSensor, INPUT);
}
void loop() {
}

the pinMode function tells Arduino that the pin (A0) will be used as an INPUT pin. Note the camelCaseUsed (see each first letter is a capital, as in it has humps, hence... camel...!) for the variables and function names. This is a convention and good to get used to.

Finally, let's use the analogRead function to get some data.

int lightSensor = A0;
void setup() {
	pinMode(lightSensor, INPUT);
}
void loop() {
	int reading = analogRead(lightSensor);
}

You'll see we stored the reading in a variable. This is important as we need to print it. Let's use the Serial library (a library is code we can add to our code to make things faster for us to write, just by calling it by its definition) to print this to the serial monitor.

int lightSensor = A0;
void setup() {
	// Set pin modes
	pinMode(lightSensor, INPUT);
	// Add the serial library
	Serial.begin(9600);
}
void loop() {
	// Read the sensor
	int reading = analogRead(lightSensor);
	// Print the value to the monitor
	Serial.print("Light: ");
	Serial.println(reading);
	// delay the next loop by 3 seconds
	delay(3000);
}

A few new things! First, you'll see these:

// This is a comment

We use comments to tell other people what our code is doing. You should use these often. The compiler won't read these and convert them in to code.

Now, we also added the Serial library with the line

Serial.begin(9600)

This is an example of a function that takes an argument. You called the library Serial then ran a function (we know it's a function because of the round braces) and passed in an integer as an argument, setting the Serial function to operate at 9600baud. Don't worry why - just know it works, for now.

The next thing we did was print to the serial monitor. We used two functions:

// This one prints to the serial with no line break (an enter at the end)
Serial.print("Light: ");
// This one puts in the line break so each time we read and write, it goes on a new line
Serial.println(reading);

What's important to see is that each has a separate purpose. Make sure your strings use double-quote marks and that you leave the space after the colon. That helps readability for the user.

Finally, we used the delay function, to slow our loop down and make it only read once every three seconds. This is written in thousands of a second. Change it to read only once every 5 seconds.

Great! On we go!

Step 6: Simulation

Always check things work by running the simulation. For this circuit, you'll also need to open the simulator to check it works and check your values.

Start the simulation and check the serial monitor. Change the value of the light sensor by clicking it and changing the value using the slider. You should see the value change in the serial monitor, too. If it doesn't, or if when you press the Start Simulation button you get errors, carefully go back and check all your code.

  • Focus on the lines indicated in the red debugging window that will be presented to you.
  • If your code is right and the simulation is still not working, check your wiring.
  • Reload the page - you might have an unrelated system/server error.
  • Shake your fist at the computer, and check again. All programmers do this. All. The. Time.

Step 7: Wire Up the Temp Sensor

I am going to assume you're on the right track now. Go ahead and wire up the temperature sensor like the picture suggests. Note the placement of the 5V and GND wires in the same space as the ones for the light. This is ok. It is like a parallel circuit and won't cause issues in the simulator. In an actual circuit, you should use a breakout board or shield to provide better power management and connections.

Now, let's update the code.

The temp sensor code

This is a little more tricky, but only because we have to do some math to convert the reading. It's not too bad.

int lightSensor = A0;
int tempSensor = A1;

void setup() {
	// Set pin modes
	pinMode(lightSensor, INPUT);
	// Add the serial library
	Serial.begin(9600);
}
void loop() {
	// The temp sensor
	// Creating two variables on one line - oh efficiency!
	// Float var to store a decimal
	float voltage, degreesC;

	// Read the value of the pin and convert it to a reading from 0 - 5
	// Essentially voltage  = (5/1023 = 0.004882814);
	voltage = (analogRead(tempSensor) * 0.004882814);

	// Convert to Degrees C
degreesC = (voltage - 0.5) * 100; // Print to the serial monitor Serial.print("Temp: "); Serial.print(degreesC); Serial.println("oC"); // Read the sensor int reading = analogRead(lightSensor); // Print the value to the monitor Serial.print("Light: "); Serial.println(reading); // delay the next loop by 3 seconds delay(3000); }

I have made some updates to the code. Let's walk through them individually.

First, I added the line

int tempSensor = A1;

Just like the lightSensor, I need to store the value in a variable to make it easier later. If I had to change the location of this sensor (like rewiring the board) then I only have to change the one line of code, not search throughout the entire codebase to change the A0 or A1, etc.

Then, we added a line to store the reading and the temp in a float. Note two variables on one line.

float voltage, degreesC;

This is really helpful because it cuts down on the number of lines I have to write and speeds up the code. It can be harder to find errors, though.

Now, we'll do the reading and store it, then convert it to our output value.

voltage = (analogRead(tempSensor) * 0.004882814);
degreesC = (voltage - 0.5) * 100;

Those two lines look difficult, but in the first we are taking the reading and multiplying it by 0.004... because it converts 1023 (the analog reading returns this value) into a reading out of 5.

The second line there multiplies that reading by 100 to move the decimal point. That gives us the temperature. Neat!

Step 8: Testing and Checking

All things going to plan, you should have a working circuit. Test by running the simulation and using the serial monitor. If you have errors, check, check again and shake your fist.

Did you make it? Share and tell us your story!

This is the final circuit embedded for you so you can play/test the final creation. Thanks for completing the tutorial!

Epilog X Contest

Participated in the
Epilog X Contest