Intro: Set Up From Scratch a Raspberry Pi to Log Data From Arduino
This tutorial is for those who have no experience with installing a new hardware, or software, let alone Python or Linux.
Let say you have ordered to Raspberry Pi (RPi) with SD card (at least 8GB, I used 16GB, type I) and power supply (5V, at least 2.5A). Today, you decide to set it up and record data from Arduinos. So let begin!
The tutorial includes setting up Raspberry Pi from the beginning, including Desktop Remote, preparing an Arduino with 18B20 temperature sensors, and executing a Python script in Raspberry Pi to collect and save the data. I updated the post with adding samba share to transfer the file between the host computer (your PC) and the Raspberry Pi
Step 1: Prepare Raspberry Pi
- Download the operating system for RPi. I recommend a full version of Raspbian, like this here .
- Assume that you have a working computer running on Windows, download Rufus software to write the image to the SD card. Scroll down and get rufus 3.0 (June 2018)
- Unzip Raspbian file (4.6 GB), and carefully select your target SD card. It takes about 3 minutes to complete writing the image to the card.
- Insert the SD card the slot, plug in power cable to the micro USB port, a Display with HDMI cable, a keyboard, a mouse is optional.
- Wait until the RPi boot up and show the desktop, press Window key, go to Accessories/Terminal and Enter.
- Configure RPi by type:
7. Change couple of thing in RPi configuration:
- In 1. Change password
In 2. Network Option: Change hostname (something unique, mine is rpi1001), and wifi SSID and password.
- In 4. Locatisation Options: Change Timezone, Keyboard, locale
- In 5. Interfacing Options: Enable SSH (to log in via command line), Enable VNC (for desktop remote)
- In 7: Advance Option: Expand Filesystems:
- After booting up: note IP address by running
If Ethernet is using, the IP should the be first block, if Wifi is used, the IP is on the third block, something like 192.168.1.40, run this command to update the Linux distro:
sudo apt-get update && sudo apt-get upgrade –y && sudo poweroff
The last one will power off the RPi after done with the updates
8. If you forgot the note the IP of RPi, or it is changed recently, use PowerShell (type PowerShell to search box in Windows)
In PowerShell to ping the Rpi: ping rpi1001.local –4 get something like this 192.168.1.40. rpi1001 is my hostname for my Rpi.
9. Install VNCViewer , this software works like TeamViewer, or Desktop Remote on Windows (only Win 10 Pro has Desktop Remote feature).
Install on your Windows machine, in the search box on the top of VNCViewer, type in the RPi’s IP (192.168.1.40) or RPi’s hostname (mine is rpi1001.local) and Enter. Enter your name RPi’s password, select ‘Remember password’ if desired so. If everything went well, you should see the pop-up Rpi desktop.
10. To transfer file from or to TeamView, the simplest way to use built-in file transfer by VNCView:
And that it, you can use a Raspberry to collect data for you, and log in to get data when needed.
Step 2: Prepare Simple Example to Collect Data From Arduino
Let say you want to collect the temperature from 4 sensors. In this example, I used 18B20, a popular temperature sensor. Other options are the TMP35,36 family or a thermistor.
The wiring is included above. The 18B20s share the wire (or bus), and here is the Arduino code on Github. The attachment in below file contains the codes and wiring map as well.
Also, install the USB driver for Arduino in Windows. Most ‘clone’ Arduino use CH341 USB driver. The driver is here.
Install driver, when plugging the Arduino to your Windows’ USB, it should recognize the driver and assign a COM port (mine is COM4)
The Serial Monitor should output like the photo above.
Step 3: Boot Up RPi by Unplug and Plug Power to RPi. Start VNCViewer, Log in Your RPi
Transfer the Python script, using VNCViewer tool. There is a banner on the near the top of the VNCViewer, look for two arrows button. To transfer data from the RPi to Windows, use the File Transfer in VNC symbol on the top-right (near the Wifi symbol) on the RPi's desktop.
Raspberry runs on Linux, and Python 2 and 3 are installed by default. You only need to install a Python library called pyserial to collect data print out from the serial line.
- to install python Serial, type this line to Linux terminal
sudo apt-get install python3-serial
- in Windows: type this PowellShell:
- pip.exe install pyserlal
Then download the script and save it to the RPi's desktop
To run the script, first make it executable by:
sudo chown u+x recordSerial.py
Upload the Arduino code to the Arduino board, then plug your USB cable with USB into, check the port by:
- On Linux, type this to the terminal: ls /dev/ttyUSB*
- On Wndows: go to Device Manager, check COM #
If the script is modified on Windows, you may need to run the Python script to dos2unix to remove weird line ending character by Windows. Install it by
sudo apt-get install dos2unix
, and convert the script by running this in the terminal
Modified the actual port in the script by a text editor:
sudo nano recordSerial.py
then run ./recordSerial.py yourfilename.txt
The Python script will save the data from the RAM to the disk for every 10 lines, which can be adjusted.
To stop recording, press Ctrl + C,
The script can be run on Windows (double click), the name of log data is the default which includes a timestamp
If you PowerShell, you could enter your customized filename,
python.exe recordSerial.py awesome.txt
Not all 18B20s are the same. See the readout!
I pushed the code to GitHub as well.
Hope this tutorial is helpful!
Step 4: Install Samba to Share Folder
This step will walk you through some basic setup to have a shared folder hosted on Raspberry Pi that can be accessed to from other computers.
First, install samba, a program to share and manage sharing folder across the network:
sudo apt-get install samba
make a shared folder
modify the configuration file for the samba by:
sudo samba /etc/samba/smb.conf
add the following lines to the end of the file:
[sambaShare]<br> comment = share Folder on Research RPI path = /home/pi/Desktop/sambaShare browseable = yes read only = no writable = yes public = yes create mask = 0777 rectory mask = 0777 guest ok = yes
If you have the problem with writing on windows, force it by adding this line to the end of the file: force user = pi
On Linux, you may need to use the root user (I will post the problem once I found out)
Next, add a user to samba and create a password as well:
sudo smbpasswd -a pi
then enter a password for the samba (can be the same or different than the password for pi user on the system)
test if the config file is okay
press Ctrl+X to save, and then restart samba service by:
sudo systemctl restart smbd
On the host computer, let say Linux:
if not yes install samba plus sambaclient, and cifs to support the share drive, please do so by running:
sudo apt-get installs samba smbclient cifs-utils
Check if the shared folder on RPI is ready by:
sudo smbclient -L yourRPI_IP
if you see the share drive, then create a mount point on Linux:
sudo mkdir /mnt/researchRPi sudo chown user:usergroup -R /mnt/researchRPI
user, usergroup is your Linux user and group name
then mount the share by:
sudo mount -f cifs -o username=pi //your_rpi_IP/sambaShare /mnt/researchRPi
enter your passwords, and make a soft link to your desktop:
sudo ln -s /mnt/researchRPi ~/Desktop/researchRPi
if you have problem with read-write permission on the share folder, experiment with a weak permission:
sudo chmod -R 776 ~/Desktop/sambaShare
On Windows, it is even easier,
Go to My Computer, and map a folder, then enter the IP of RPI, the shared folder should appear. I believe there is something on Mac that you can browse the shared folder on the network.
Step 5: Useful References
Below are some useful references:
- There is another approach here using pyserial and numpy libraries
- A comprehensive look at the electronic communication starts from installing Arduino IDE. A good reference if you want a deeper dive.
- A short but concrete writing. A short and sufficient options for alternatives to log the data