Lesson 106-1: Reading Voltage from a Potentiometer and Recording it on Micro SD
In this tutorial, we will learn how to read voltage from a potentiometer using an Arduino and record the data onto a microSD card. This is particularly useful for data logging applications, where you want to store sensor readings for later analysis. By the end of this project, you will have a system that logs voltage readings and can display them on a serial monitor when prompted by a push button. For further clarification, please refer to the video (in video at 00:00).Hardware Explained
To complete this project, we will use an Arduino board, a microSD card module, a potentiometer, and a push button. The Arduino acts as the central controller, reading the voltage levels from the potentiometer and managing the data logging process. The microSD card module allows for storage of the logged data in a CSV format, making it easy to access with spreadsheet software. The potentiometer is a variable resistor that provides an adjustable voltage output, which we will read through one of the analog pins on the Arduino. The push button serves as a trigger to read and display the logged data from the microSD card. The microSD card module communicates with the Arduino using SPI (Serial Peripheral Interface), allowing for efficient data transfer.Datasheet Details
| Manufacturer | Microchip Technology |
|---|---|
| Part number | SD Card Module |
| Logic/IO voltage | 3.3 V |
| Supply voltage | 5 V |
| Output current (per channel) | 0.5 A |
| Peak current (per channel) | 2 A |
| PWM frequency guidance | N/A |
| Input logic thresholds | 0.3 V (low), 2.4 V (high) |
| Voltage drop / RDS(on) / saturation | 0.2 V |
| Thermal limits | 85 °C max |
| Package | Module |
| Notes / variants | Compatible with various Arduino models |
- Ensure the SD card is formatted as FAT32 for compatibility.
- Use appropriate voltage levels; 3.3 V for the SD card, but 5 V for Arduino.
- Check wiring connections to avoid communication errors.
- Implement pull-up resistors for push buttons to prevent floating inputs.
- Limit file names to a maximum of 8 characters for compatibility.
Wiring Instructions
To wire the components, start by connecting the microSD card module. Connect the `VCC` pin to the 5V pin on the Arduino and the `GND` pin to the ground. The `MISO` pin of the microSD module should go to pin 12 on the Arduino, while the `MOSI` pin should connect to pin 11. The `CLK` pin connects to pin 13, and finally, the `CS` (Chip Select) pin connects to pin 4. Next, for the potentiometer, connect one outer pin to the ground and the other outer pin to the 3.3V supply. The middle pin of the potentiometer should connect to the `A0` pin on the Arduino to read its voltage. For the push button, connect one side to pin 9 on the Arduino and the other side to ground. Make sure to use the internal pull-up resistor for the button to avoid floating states.Code Examples & Walkthrough
The main program uses the SD library and SPI library to manage the microSD card and read data from the potentiometer. Here’s a brief look at how it initializes and sets up the logging feature:void setup() {
Serial.begin(9600);
while (!Serial) { ; } // wait for serial port to connect
pinMode(readPushButtonPin, INPUT_PULLUP);
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
SD.remove(logFile);
myFile = SD.open(logFile, FILE_WRITE);
}
This setup function initializes serial communication and the SD card, ensuring that the necessary components are ready for data logging.
Another crucial part of the code is the loop function, where the voltage is read and logged:
void loop() {
int sensor = analogRead(A0); // read A0 pin
float voltage = sensor * (5.0 / 1023.0);
if (digitalRead(readPushButtonPin) == LOW) {
delay(100);
readLog();
}
writLog(voltage);
}
In this loop, the analog value from the potentiometer is read, converted to voltage, and logged to the microSD card. If the push button is pressed, it triggers the reading of logged data.
Demonstration / What to Expect
When the setup is complete, rotating the potentiometer will change the voltage reading, and this data will be recorded onto the microSD card. Pressing the push button will display the logged data on the serial monitor. If everything is wired correctly, you should see varying voltage values corresponding to the potentiometer’s position. Common pitfalls include incorrect wiring, especially with the microSD module and potential voltage mismatches (in video at 12:34).Video Timestamps
- 00:00 Introduction
- 02:38 SPI communication in Arduino
- 04:46 Schematic for SD Card module
- 05:47 SD card file format
- 06:35 SD library for Arduino
- 08:35 SD Card Wiring
- 09:53 Code Explained (Basic read and write)
- 16:53 Project 2: Logging voltage reading
- 17:29 Wiring with potentiometer and push button
- 24:15 Example: SD Card info
- 25:20 Example: List Files
- 28:57 Example: Nonblocking Write
- 31:42 Project 3: Data logging in CSV mode for Excel
- 35:47 Project 3: Demonstration
396-Lesson 106: Using a Micro SD Card and Data Logging with Arduino
Language: C++
/*
Original from File->Examples->SD SD card read/write
* Lesson 106-1: Reading voltage from potentiometer and saving it on microSD card
In this example we read A0 pin to read potentiometer's voltage
and writing it to the Micro SD card.
We have also a push button, when pressed, it will print the
file data on the serial monitor.
In the lesson https://youtu.be/TduSOX6CMr4
Example 1: Card info demonstrated
Example 2: file list demonstrated
Example 3: Datalogger
Example 4: DoNotBlockWrite
Example 5 (this code): Reading potentiometer saving on micro SD card and using push button to read it
Example 6: Using example 5 but creating CSV file so we can open it on Excel
Example 7: Writing into two files at the same time in micro SD Card
* Watch Video instruction for this code:https://youtu.be/TduSOX6CMr4
*
* This code is part of Arduino Step by Step Course which starts here: https://youtu.be/-6qSrDUA5a8
*
* for library of this code visit http://robojax.com/
*
If you found this tutorial helpful, please support me so I can continue creating
content like this. Make a donation using PayPal by credit card https://bit.ly/donate-robojax
written/updated by Ahmad Shamshiri on Jun 18, 2022
www.Robojax.com
* * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
* This code has been downloaded from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
created Nov 2010
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include <SPI.h>
#include <SD.h>
//serial monitor print while writing log
boolean serialPrint = true;//true or false
char *logFile ="datalog.txt";//file name. must be max 8 character excluding the extension (book.txt is 4 char)
int readPushButtonPin = 9;
File myFile;
void writLog(float);//prototype of writLog funciton at the bottom of this code
void readLog(void);//prototype of readLog funciton at the bottom of this code
void setup() {
//Watch the instruction to this code on YouTube https://youtu.be/TduSOX6CMr4
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
pinMode(readPushButtonPin, INPUT_PULLUP);
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
SD.remove(logFile);
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open(logFile, FILE_WRITE);
if (!myFile) {
Serial.println("log file missing");
while(1);
}
}
void loop() {
int sensor = analogRead(A0);//read A0 pin
//convert value of 0 to 1023 to voltage.
//Arduino UNO is 5V so we use 5
float voltage = sensor * (5.0 / 1023.0);
if(digitalRead(readPushButtonPin) == LOW)
{
delay(100);//
readLog();
}
writLog(voltage);
//Watch the instruction to this code on YouTube https://youtu.be/TduSOX6CMr4
}
/*
*/
void writLog(float voltage)
{
myFile = SD.open(logFile, FILE_WRITE);
//Watch the instruction to this code on YouTube https://youtu.be/TduSOX6CMr4
myFile.print("Time: ");//writing the text: Time:
myFile.print(millis()/1000); //record time
myFile.print("\tVoltage: ");//writing the text Voltage after a tab \t
myFile.print(voltage);//Writing voltage
myFile.println("V");//adding V at the end
if(serialPrint){
Serial.print ("Time:");
Serial.print(millis()/1000);
Serial.print ("\tVoltage: ");
Serial.print(voltage);
}
// close the file:
myFile.close();
if(serialPrint){
Serial.println(" done.");
}
}//writLog
/*
* readLog
* Reads the data from MicroSD Card and prints it on serial monitor
*/
void readLog()
{
//Watch the instruction to this code on YouTube https://youtu.be/TduSOX6CMr4
// re-open the file for reading:
myFile = SD.open(logFile);
if (myFile) {
Serial.print("Reading ========");
Serial.println(logFile);
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
Serial.print("Reading END==========");
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.print("readLog() error opening ");
Serial.println(logFile);
}
//Watch the instruction to this code on YouTube https://youtu.be/TduSOX6CMr4
}
Things you might need
-
AliExpressPurchase a microSD card module from AliExpresss.click.aliexpress.com
-
BanggoodPurchase a microSD card module from Banggoodbanggood.com
Resources & references
-
External
-
ExternalPurchase a microSD card module from AliExpresss.click.aliexpress.com
-
External
-
External
-
ExternalPurchase a microSD card module from Banggoodbanggood.com
-
External
-
External
-
External
Files📁
No files available.