How to Get Sensor's Data Graph? (Rpi Pico + Python)
by Prachet Hure in Circuits > Raspberry Pi
3769 Views, 6 Favorites, 0 Comments
How to Get Sensor's Data Graph? (Rpi Pico + Python)
Suppose you have programmed a microcontroller to read and print the sensor data. It may can be any sensor that outputs a number proportional to the sensed parameter like heat, light, or sound. At this stage you may work with the live data generated by the sensor (for eg- turn on a device if sensor value falls below a set value) but what if you want to log the data and represent it in a form of a graph, then you are on the right webpage. Get the components and follow the below given steps.
Supplies
Components:-
- Raspberry pi pico.
- Button switch.
- Resistors 6.8K and 22K.
- LDR (or any sensor of your choice. I am using LDR just for demo.)
- USB cable.
If you are using Rpi pico for the first time. I recommend you to watch "Getting started with Rpi pico."
Make the Circuit.
Make the connections as per the circuit diagram if you want to use LDR as a sensor. +5V DC is provided between VSYS pin and GND pin of Rpi pico through USB cable. For now the board is connected to the computer because we want to see sensor data and print status on the shell / console. Rpi pico operates on 3.3V but we don't need a 3.3V regulator cause Rpi pico has a in-buit buck boost converter that converts input voltages (1.8V - 5.5V) to 3.3V. The circuit operating voltage is also kept at 3.3V because the ADC reads 3.3V as maximum value.
Testing the Circuit by Running Test Code.
This step is just to ensure that all the components used in the circuit are working as per expectation. Also you will get to know how the code works.
Copy the below given code from "code.txt". paste it in the shell window of thony IDE. Make sure that LDR is exposed to light. Hit enter. If there was enough light you may see ADC value greater than 40000, printed on the next line.
Now cover the sensor with hand or make it dark by turning off the lights. Run the same code again you may see ADC value less than 10000, printed on the next line.
Run content of "code2.txt" file and press the switch. This code will print the ADC value at the instance of switch press and stop running. Once you are done with testing circuit and understanding the code you are free to move on to the next step.
Getting Graph Using Quickchart API.
API stands for "Application Program Interface". It's the link between your program and website or any web based application. The website will give you data in response if you requested providing proper parameters and auth key.
Quickchart is a open source web service that generates all types of graphs like line graph, bar graph, pie diagram, etc. All we need to do is post a request with quickchart's endpoint URL and our data enclosed in a JSON object. If our post is successful then we get the chart's URL in response. The chart generated by quickcharts free version are public domain, meaning you are free to use them for any purpose. To know more read quickchart's documentation .
Click on the below given URL to see how generated chart looks like. It's a bar graph.
https://quickchart.io/chart?c={type:'bar',data:{la...
In the above URL, "https://quickchart.io/chart" is API endpoint and everything after "?" is parameters. parameter "c" holds our chart data (for eg- Type of chart, X axis data, Y axis data, etc) as JSON object. JSON objects are easy to read and write using JSON module which creates ".json" files.
The best thing is that, json module/package is available in python 3.10 as well as in micropython. Let's now see how to create ".json" file (that has our sensor data) using Rpi pico.
Creating JSON File in Micropython.
JSON stand for "Java Script Object Notation". It is common format to send data across the internet or from one application to another. It has a format similar to a python dictionary. The data is enclosed in curly braces like this {data}". If there are many categories then data maybe a "key:value" pair. The value can either be a list or a dictionary. It may look confusing but it's good for programmers as they can easily dig through and get the desired data. It's good for computers but not for humans who are used to seeing data in form of tables, graphs and diagrams.
The above image shows quickcharts json object. Let's take a look at that and see where can we put our sensor data. The "chart_data" is a dictionary between two curly braces there are many "key":"value" pairs like "backgroundColor": "white", "width": 800. These parameters have fix value. All we need to change is the "data" key's value, that value consists of two lists namely "labels" and "datasets". "labels" is X-axis data therefore it will contain list of sample count [1,2,3,4,....max sample count]. The data corresponding to each sample is our sensor data or Y-axis data.
"datasets" list is a list of dictionaries and currently has only one dictionary because right now we are dealing with only one Y-axis parameter, We have only one sensor. In that dictionary there is "data" key and it's value is a list and that list is our Y-axis data.
Now that we have figured out were to put our sensor data. Let's create two lists, one will contain the sensor data and the other will contain the sample count.
Once we have the two lists we can create a function that creates ".json" file and that two lists which contain the X-axis and Y-axis data can be passed as arguments. This function will be called in the end after the sensor data and sample count are aquired in form of two separate lists.
".json" file will be created in the same directory/folder as that of the program file "main.py". The program file is provided below as "main_prog.txt"
*note - The program will print status "json file created" and will stop. To take new samples the program has to run again. There are some limitations to the max samples due to two reasons which will be discussed at the end of this blog.
Downloads
Python Program for Reading JSON File and Making Request to Quickchart API.
Once the JSON file is created it's stored in Rpi pico's flash memory. Now all that is left to do is read that json file using Python 3.10 and post a request to quickchart's endpoint API. For that we may need another python IDE like IDLE or PyCharm. I am using PyCharm, you can use any IDE of your choice but before running the program, install "requests" package.
The "requests" package is responsible for making request to API and getting the JSON response. The "requests" package is not available in micropython for Rpi pico that's the reason we have to use another IDE to post a request to API. Follow the below given steps. Watch the above given YouTube video.
- Copy the program from the below given "get_chat_url.txt" file. Create new project in PyCharm create "main.py" and paste the program.
- Open thonny IDE and connect Rpi pico to USB com port. The code starts running, if you want to record new data then hit the switch, else if you want old data then stop the running code by clicking on the stop icon.
- Click on file > Click on open > Select Raspberri pi pico > Open the JSON file > Click on file > Click on save copy > Click on "This computer" > Go to the file location of "main.py" that we made on pycharm > hit ok.
- Now that our JSON file and "main.py" are in the same project folder, program can access JSON file.
- Run main.py. If request is successful then we get the chart URL printed in the console.
- Click on the URL and view your data presented wonderfully on a graph.
- You can now download the chart image as ".png" file.
Downloads
Understanding Limitations.
Following are the limitations:
- Maximum number of samples per graph is 241: Maximum number of data we can pass to get a graph is limited to 241 samples. If you want output of better resolution then you may think of increasing the number of samples greater than 241 but then you have to modify the code to generate another graph and merge those two graphs using photoshop. In my program, you can set the number of samples for single graph (1 to 241) depending on the duration of your signal.
- Flash memory of 2MB: Memory is another factor which restricts the maximum samples. You may generate more than one graph to fit in all the samples but the data is stored in Rpi pico's flash memory which is of 2MB. This means, size of single sample times number of samples cannot exceed the flash memory size. This has to be fixed by interfacing external memory to Rpi pico if you want to log a large amount of data.
- Sample period (Tp): Tp is the time interval between two samples. The sampling rate of ADC in Rpi pico is 500 ksps (kilo samples per second). That means 500,000 samples per second. If we divide 1 sec by 500,000 then we get 2 microseconds. This is the maximum time interval between two samples. This means, any periodic signal let it be a sine wave or a square wave having frequency greater than 250KHz will not be reconstructed because according to nyquist's criteria the sampling frequency has to be greater than twice the signal frequency in order for the signal to be properly constructed. This has to be understood because it will save us some data space. For eg - If the sensor's signal frequency is >1Hz then you can keep the sampling frequency 3Hz(min - less resolution) to maximum frequency of your choice depending on the memory constrains. I personally would keep sampling frequency 10Hz - 20Hz for a 1Hz signal if the signal accuracy is not a big deal. In my program, I have kept the Tp variable, you can set it's value depending on the rate of signal change.