[PYTHON] Working with sensors on Mathematica on Raspberry Pi

Raspberry Pi (hereinafter referred to as Raspberry Pi) can be used free of charge by Mathematica. With the Raspberry Pi 4, the processing power of the CPU has increased, so you don't have to wait endlessly for 3D drawing. Raspberry Pi is a Linux machine that can access hardware such as GPIO, I2C, and SPI. Raspbian Buster is now available from June 2019.

Launch Mathematica from the terminal

Run sudo Mathematica to launch Mathematica with root privileges. Normally, pi users can access GPIO, I2C, and SPI without root privileges. Here, Mathematica reads the temperature from the MEMS sensor BME280 and graphs it.

BME280 device driver registration

Raspberry Pi comes with a number of pre-compiled sensor device drivers. Details can be found in / boot / overlays / README. It's just a brief explanation. Most of the drivers are iio. First, enable the I2C interface by clicking Raspberry Pi Configuration from Preferences in the main menu, selecting the Interface tab, checking I2C and pressing OK. From Buster, no reboot is needed. ras202-e0df932a.png

  sudo nano /boot/config.txt

Then open the config file config.txt and on the last line,

  dtoverlay = i2c-sensor,bme280,param=0x76

Add, save by overwriting, exit, reboot, and the BME280 will be enabled on the I2C interface. Confirmation is OK when i2cdetect -y 1 is executed and UU is displayed at 0x76. bme01.png Before that, connect the sensor.

BME280 purchased at Amazon

It was 820 yen and it was delivered the next day.

"HiLetgo BME280 temperature sensor humidity sensor pressure sensor Arduino sensor atmospheric pressure sensor temperature and humidity sensor breakout Arduino compatible [parallel import goods]"

Connect with I2C as follows. The BME280 itself supports both SPI and I2C, but this board is dedicated to I2C and the device driver is also for I2C. BME280b-400wri.png

Reading temperature data

When the device driver is installed, the data read under / sys is a text file and is updated from time to time. ras208.png

Read as follows: It is an integer and has a value of 1000 times.

  cat /sys/bus/i2c/devices/1-0076/iio:device0/in_temp_input

In python,

f = open('/sys/bus/i2c/devices/1-0076/iio:device0/in_temp_input')
Temp = round(int(f.read()) / 1000.0, 1)
f.close
print(Temp)

You can read it with.

Use Python programs with Mathematica

To use Python programs from Mathematica, use the External Sessions feature. Python programs are functions. Call that function with ExternalValue and receive the value.

dataList = {};
Do[
session = StartExternalSession["Python"];
 
ExternalEvaluate[session, "def temp():
    f = open('/sys/bus/i2c/devices/1-0076/iio:device0/in_temp_input');
    Temp = round(int(f.read()) / 1000.0, 1);
    f.close;
    return Temp
"];
 
v = ExternalValue[session, "temp()"];
DeleteObject[session];
AppendTo[dataList, v]
, {v, 1, 10}
]
dataList
ListLinePlot[dataList]

It looks like it is running. I read it 10 times and drew a graph. math101.png

Please refer to https://www.denshi.club/pc/raspi/linux-3-mathematicabme280.html for the program that updates the graph in real time.

Recommended Posts

Working with sensors on Mathematica on Raspberry Pi
Working with GPS on Raspberry Pi 3 Python
Programming normally with Node-RED programming on Raspberry Pi 3
GPGPU with Raspberry Pi
pigpio on Raspberry pi
DigitalSignage with Raspberry Pi
Cython on Raspberry Pi
Enjoy electronic work with GPIO on Raspberry Pi
Power on / off your PC with raspberry pi
Play with your Ubuntu desktop on your Raspberry Pi 4
Connect to MySQL with Python on Raspberry Pi
Record temperature and humidity with systemd on Raspberry Pi
Run LEDmatrix interactively with Raspberry Pi 3B + on Slackbot
Try debugging Python on Raspberry Pi with Visual Studio.
Control brushless motors with GPIOs on Raspberry Pi Zero
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Introduced pyenv on Raspberry Pi
Troubleshoot with installing OpenCV on Raspberry Pi and capturing
Use NeoPixel on Raspberry Pi
Install OpenCV4 on Raspberry Pi 3
Install TensorFlow 1.15.0 on Raspberry Pi
[Raspberry Pi] Stepping motor control with Raspberry Pi
Testing uart communication on Raspberry Pi
Use vl53l0x with Raspberry Pi (python)
Servo motor control with Raspberry Pi
raspberry pi 4 centos7 install on docker
Serial communication with Raspberry Pi + PySerial
Install ghoto2 on Raspberry Pi (memo)
OS setup with Raspberry Pi Imager
Try L Chika with raspberry pi
OpenCV installation procedure on Raspberry Pi
VPN server construction with Raspberry Pi
Try moving 3 servos with Raspberry Pi
Power on / off Raspberry pi on Arduino
Detect switch status on Raspberry Pi 3
Install OpenMedia Vault 5 on Raspberry Pi 4
Using a webcam with Raspberry Pi
L Chika on Raspberry Pi C #
Build wxPython on Ubuntu 20.04 on raspberry pi 4
Control the motor with a motor driver using python on Raspberry Pi 3!
Measure SIM signal strength with Raspberry Pi
Pet monitoring with Rekognition and Raspberry pi
"Honwaka Notification Lamp" on Raspberry Pi Part 2
Detect "brightness" using python on Raspberry Pi 3!
USB boot on Raspberry Pi 4 Model B
Hello World with Raspberry Pi + Minecraft Pi Edition
"Honwaka Notification Lamp" on Raspberry Pi Part 1
Build a Tensorflow environment with Raspberry Pi [2020]
Enable UART + serial communication on Raspberry Pi
Adafruit Python BluefruitLE works on Raspberry Pi.
Accelerate Deep Learning on Raspberry Pi 4 CPU
Set swap space on Ubuntu on Raspberry Pi
Use the Grove sensor on the Raspberry Pi
Install 64-bit OS (bate) on Raspberry Pi
Improved motion sensor made with Raspberry Pi
Install docker-compose on 64-bit Raspberry Pi OS
Try Object detection with Raspberry Pi 4 + Coral
Run servomotor on Raspberry Pi 3 using python
"Honwaka Notification Lamp" on Raspberry Pi Part 3
Power SG-90 servo motor with raspberry pi
Use PIR motion sensor with raspberry Pi