Food Irradiation Dose Detection - DFRobot Beetle ESP32C3
Building a food irradiation detection device using a DFRobot ESP32, Geiger Counter, and visible light sensor.
Last updated
Building a food irradiation detection device using a DFRobot ESP32, Geiger Counter, and visible light sensor.
Last updated
Created By: Kutluhan Aktar
Public Project Link: https://studio.edgeimpulse.com/public/109647/latest
Even though food irradiation improves food hygiene, spoilage reduction, and extension of shelf-life, it should be regulated strictly to avoid any health risks and nutritional value drops. However, small businesses in the food industry lack a budget-friendly and simple way to detect food irradiation doses after treating food with ionizing energy, especially for animal (livestock) feed. Therefore, I decided to build an AI-driven IoT device predicting food irradiation doses based on weight, color (visible light), and emitted ionizing radiation.
Ionizing radiation is a nonthermal process utilized to achieve the preservation of food. At a maximum commercial irradiation dose of 10 kGy, irradiation does not impart heat to the food, and the nutritional quality of the food is generally unaffected. The irradiation process can reduce the microbial contamination of food, resulting in improved microbial safety as well as the extended shelf-life of the food[^1]. Irradiation also benefits the consumer by reducing the risk of severe health issues caused by foodborne illnesses. Food irradiation has three categories: low-dose (radurization), medium-dose (radicidation), and high-dose (radappertization). Low dose irradiation (under 1 kGy) inhibits the sprouting of produce (onion, potato, and garlic); retards the ripening and fungi deterioration of fruits and vegetables (strawberry, tomato, etc.), and promotes insect disinfestations in cereals and vegetables. Medium dose irradiation (between 1 and 10 kGy) controls the presence of pathogenic organisms, especially in fruit juices; retards the deterioration of fish and fresh meat; and reduces Salmonella in poultry products, similar to pasteurization. High dose irradiation (over 10 kGy) is rather significant to the sterilization of health and personal hygiene products[^2].
Since foods treated with ionizing radiation should be adequately labeled under the general labeling requirements, consumers can make their own free choice between irradiated and non-irradiated food. However, unfortunately, some countries do not apply strict regulations for irradiated foods, especially for animal feed. Therefore, detecting proper irradiation doses can be arduous for small businesses in the food industry due to governments not incentivizing strictly regulated food irradiation processes. Since irradiation can engender certain alterations that can modify the chemical composition and nutritive values of food, depending on the factors such as irradiation dose, food composition, packaging, and processing conditions such as temperature and atmospheric oxygen saturation[^2], unsupervised food irradiation portends health issues.
After scrutinizing recent research papers on food irradiation, I decided to utilize ionizing radiation, weight, and visible light (color) measurements denoting the applied irradiation dose so as to create a budget-friendly and accessible device to predict food irradiation dose levels in the hope of assisting small businesses in checking compliance with existing regulations on food irradiation.
Although ionizing radiation, weight, and visible light (color) measurements provide insight into detecting food irradiation doses, it is not possible to conclude and interpret food irradiation doses precisely by merely employing limited data without applying complex algorithms since food irradiation dose levels fluctuate depending on processing techniques, food characteristics, and equipment. Therefore, I decided to build and train an artificial neural network model by utilizing the theoretically assigned food irradiation dose classes to predict food irradiation dose levels based on ionizing radiation, weight, and visible light (color) measurements. Since I could not apply ionizing radiation directly to foods by emitting Gamma rays, X-rays, or electron beams, I exposed foods to sun rays as a natural source of radiation for estimated periods.
Since Beetle ESP32-C3 is an ultra-small size development board intended for IoT applications, that can easily collect data and run my neural network model after being trained to predict food irradiation doses, I decided to employ Beetle ESP32-C3 in this project. To obtain the required measurements to train my model, I utilized a Geiger counter module (Gravity), an I2C weight sensor (Gravity), and an AS7341 11-channel visible light sensor (Gravity). Since Beetle ESP32-C3 is equipped with an expansion board providing the GDI display interface, I connected an SSD1309 OLED transparent screen (Fermion) to display the collected data.
After collecting data successfully, I developed a PHP web application that obtains the transmitted data from Beetle ESP32-C3 via HTTP GET requests, logs the received measurements in a given MySQL database table, and lets the user create appropriately formatted samples for Edge Impulse.
After completing my data set and creating samples, I built my artificial neural network model (ANN) with Edge Impulse to make predictions on food irradiation dose levels (classes) based on ionizing radiation, weight, and visible light (color) measurements. Since Edge Impulse is nearly compatible with all microcontrollers and development boards, I had not encountered any issues while uploading and running my model on Beetle ESP32-C3. As labels, I employed the theoretically assigned food irradiation dose classes for each data record while collecting and logging data:
Regulated
Unsafe
Hazardous
After training and testing my neural network model, I deployed and uploaded the model on Beetle ESP32-C3. Therefore, the device is capable of detecting precise food irradiation dose levels (classes) by running the model independently without any additional procedures.
Lastly, to make the device as robust and compact as possible while experimenting with a motley collection of foods, I designed a Hulk-inspired structure with a movable visible light sensor handle (3D printable).
So, this is my project in a nutshell 😃
In the following steps, you can find more detailed information on coding, logging data via a web application, building a neural network model with Edge Impulse, and running it on Beetle ESP32-C3.
🎁🎨 Huge thanks to DFRobot for sponsoring these products:
⭐ Beetle ESP32-C3 | Inspect
⭐ Gravity: Geiger Counter Module | Inspect
⭐ Gravity: I2C 1Kg Weight Sensor Kit | Inspect
⭐ Gravity: AS7341 11-Channel Visible Light Sensor | Inspect
⭐ Fermion: 1.51” OLED Transparent Display | Inspect
🎁🎨 If you want to purchase products from DFRobot, you can use my $5 discount coupon.
🎁🎨 Also, huge thanks to Creality for sending me a Creality CR-200B 3D Printer.
Since this project is for detecting irradiation doses of foods treated with ionizing radiation, I got inspired by the most prominent fictional Gamma radiation expert, Bruce Banner (aka, The Incredible Hulk), to design a unique structure so as to create a robust and compact device flawlessly operating while collecting data from foods. To collect data with the visible light sensor at different angles, I added a movable handle to the structure, including a slot and a hook for hanging the sensor.
I designed the structure and its movable handle in Autodesk Fusion 360. You can download their STL files below.
For the Hulk replica affixed to the top of the structure, I utilized this model from Thingiverse:
Then, I sliced all 3D models (STL files) in Ultimaker Cura.
Since I wanted to create a solid structure for this device with a movable handle and complement the Hulk theme gloriously, I utilized these PLA filaments:
eMarble Natural
Peak Green
Finally, I printed all parts (models) with my Creality CR-200B 3D Printer. It is my first fully-enclosed FDM 3D printer, and I must say that I got excellent prints effortlessly with the CR-200B :)
If you are a maker planning to print your 3D models to create more complex projects, I highly recommend the CR-200B. Since the CR-200B is fully-enclosed, you can print high-resolution 3D models with PLA and ABS filaments. Also, it has a smart filament runout sensor and the resume printing option for power failures.
According to my experience, there are only two downsides of the CR-200B: relatively small build size (200 x 200 x 200 mm) and manual leveling. Conversely, thanks to the large leveling nuts and assisted leveling, I was able to level the bed and start printing my first model in less than 30 minutes.
#️⃣ Before the first use, remove unnecessary cable ties and apply grease to the rails.
#️⃣ Test the nozzle and hot bed temperatures.
#️⃣ Go to Settings ➡ Leveling and adjust four predefined points by utilizing the leveling nuts.
#️⃣ Finally, attach the spool holder and feed the extruder with the filament.
#️⃣ Since the CR-200B is not officially supported by Cura, select the Ender-3 profile and change the build size to 200 x 200 x 200 mm. Also, to compensate for the nozzle placement, set the Nozzle offset X and Y values to -10 mm on the Extruder 1 tab.
First of all, I soldered male pin headers to Beetle ESP32-C3 and its expansion board.
Then, to collect ionizing radiation, weight, and color (visible light) measurements, I connected a Geiger counter module (Gravity), an I2C HX711 weight sensor (Gravity), and an AS7341 11-channel visible light sensor (Gravity) to Beetle ESP32-C3. Since the expansion board provides the GDI display interface for DFRobot screens, I was able to connect the SSD1309 OLED transparent screen (Fermion) to Beetle ESP32-C3 via the expansion board.
After assembling the weight sensor kit, to calibrate the weight sensor in order to get accurate measurements, press the cal button on the adapter board. Then, wait for the indicator LED to turn on and place a 100 g (default value) object on the scale within 5 seconds. When the adapter board completes calibration, the indicator LED blinks three times.
Since Beetle ESP32-C3 cannot power the Geiger counter module and the weight sensor simultaneously due to its working current, I connected a USB buck-boost converter board to my Xiaomi power bank to elicit stable 3.3V to supply the sensors.
Since the Geiger counter library needs to use an external interrupt pin for counting, the Geiger counter module can only be connected to external interrupt pins. Plausibly, Beetle ESP32-C3 allows the user to define any pin as an external interrupt.
To assign labels while transmitting the collected data and run my neural network model effortlessly, I added three control buttons (6x6), as shown in the schematic below.
After completing sensor connections and adjustments on breadboards successfully, I made the breadboard connection points rigid by utilizing a hot glue gun.
After printing all parts (models), I fastened all components except the visible light sensor to their corresponding slots on the structure via the hot glue gun.
Then, I attached the visible light sensor to the movable handle and hung it via its slot in the structure.
Finally, I affixed the Hulk replica to the top of the structure via the hot glue gun.
To be able to log and process data packets transmitted by Beetle ESP32-C3, I decided to develop a web application in PHP named food_irradiation_data_logger.
As shown below, the web application consists of two folders and five files:
/assets
class.php
icon.png
index.css
/data
get_data.php
index.php
I also employed the web application to scale (normalize) and preprocess my data set so as to create appropriately formatted samples for Edge Impulse.
If the data type is not time series, Edge Impulse requires a CSV file with a header indicating data fields per sample to upload data with CSV files. Since Edge Impulse can infer the uploaded sample's label from its file name, the application reads the given data set in the MySQL database and generate a CSV file (sample) for each data record, named according to the assigned food irradiation dose class. Also, the application utilizes the unique row number under the id data field as the sample number to identify each generated CSV file:
Regulated.training.sample_101.csv
Unsafe.training.sample_542.csv
Hazardous.training.sample_152.csv
You can download and inspect the web application in the ZIP file format below.
📁 class.php
In the class.php file, in order to run all functions successfully, I created two classes named _main and sample: the latter inherits from the former.
⭐ Define the _main class and its functions:
⭐ In the init function, define the required variables for the MySQL database.
⭐ In the insert_new_data function, append the given measurements and food irradiation dose class to the given database table.
⭐ In the database_create_table function, create the required database table.
⭐ Define the sample class, extending the _main class, and its functions:
⭐ Define the food irradiation dose class (label) names.
⭐ In the count_samples function, count the registered data records (samples) in the given database table.
⭐ In the create_sample_files function:
⭐ Obtain the registered data records from the given database table.
⭐ Scale (normalize) data items to define appropriately formatted inputs in the range of 0-1.
⭐ Define the header indicating data elements.
⭐ Create an array with the scaled data items.
⭐ For each data record, create a CSV file (sample) named with the assigned irradiation dose class and identified with the unique row number under the id data field.
⭐ Each sample includes twelve data items [shape=(12,)]:
[15.877, 0.25, 0.76, 0.57, 0.8, 1.89, 2.85, 4.65, 3.63, 0.8, 5.31, 0.53]
⭐ In the download_samples function, download all generated CSV files (samples) in the ZIP file format.
⭐ Define the required MySQL database connection settings for Raspberry Pi.
📁 get_data.php
⭐ Include the class.php file.
⭐ Define the food object of the _main class with its required parameters.
⭐ Obtain the transferred information from Beetle ESP32-C3.
⭐ Then, insert the received measurements into the given database table.
⭐ If requested, create the required database table (entries).
📁 index.php
⭐ Include the class.php file.
⭐ Define the sample object of the sample class with its required parameters.
⭐ Elicit the total number of data records (samples) for classes (labels) in the given database table.
⭐ If the user requests via the HTML form, create a CSV file (sample) for each data record in the given database table, depending on the selected data type: training or testing.
⭐ If the Download button is clicked, download all generated CSV files (samples) in the ZIP file format.
Since I decided to host my web application on a Raspberry Pi 3, I needed to set up a LAMP web server.
#️⃣ First of all, open a terminal window by selecting Accessories ➡ Terminal from the menu.
#️⃣ Then, install the apache2 package by typing the following command into the terminal and pressing Enter:
sudo apt-get install apache2 -y
#️⃣ After installing the apache2 package successfully, open Chromium Web Browser and navigate to localhost so as to test the web server.
#️⃣ Then, enter the command below to the terminal to obtain the Raspberry Pi's IP address:
hostname -I
#️⃣ To install the latest package versions successfully, update the Pi. Then, download the PHP package by entering these commands below to the terminal:
sudo apt-get update
sudo apt-get install php -y
#️⃣ To be able to create files in the ZIP file format with the web application, install the php-zip package:
sudo apt install php7.3-zip
#️⃣ Since the web application creates a large ZIP file with the generated CSV files (samples), open the php.ini file in order to modify these configurations:
upload_max_filesize
max_file_uploads
#️⃣ Then, restart the apache server to activate the installed packages on the web server:
sudo service apache2 restart
Since I needed to log measurements transmitted by Beetle ESP32-C3 so as to create appropriately formatted samples for Edge Impulse, I also set up a MariaDB server on Raspberry Pi 3.
#️⃣ First of all, install the MariaDB (MySQL) server and PHP-MySQL packages by entering the following command into the terminal:
sudo apt-get install mariadb-server php-mysql -y
#️⃣ To create a new user, run the MySQL secure installation command in the terminal window:
sudo mysql_secure_installation
#️⃣ When requested, type the current password for the root user (enter for none). Then, press Enter.
#️⃣ Type in Y and press Enter to set the root password.
#️⃣ Type in bot at the New password: prompt, and press Enter.
#️⃣ Type in Y to remove anonymous users.
#️⃣ Type in Y to disallow root login remotely.
#️⃣ Type in Y to remove the test database and its access permissions.
#️⃣ Type in Y to reload privilege tables.
#️⃣ After successfully setting the MariaDB server, the terminal prints: All done! Thanks for using MariaDB!
#️⃣ Finally, to create a new database in the MariaDB server, run the MySQL interface in the terminal:
sudo mysql -uroot -p
#️⃣ Then, enter the recently changed root password - bot.
#️⃣ When the terminal shows the MariaDB [(none)]> prompt, create the new database (foodirradiation) by utilizing these commands below:
#️⃣ Press Ctrl + D to exit the MariaDB [(none)]> prompt.
As discussed above, I set up a LAMP web server on my Raspberry Pi 3 to run the web application, but you can run it on any server as long as it is a PHP server.
#️⃣ First of all, install and extract the food_irradiation_data_logger.zip folder.
#️⃣ Then, move the application folder (food_irradiation_data_logger) to the Apache server (/var/www/html) by using the terminal since the Apache server is a protected location.
sudo mv /home/pi/Downloads/food_irradiation_data_logger /var/www/html/
#️⃣ Since the Apache server is a protected location, it throws an error while attempting to modify the files and folders in it. Therefore, before utilizing the web application to create CSV files (samples) and download them in the ZIP file format, change the web application's folder permission by using the terminal:
sudo chmod -R 777 /var/www/html/food_irradiation_data_logger
💻 On the get_data.php file:
⭐ If the web application did not receive measurements from Beetle ESP32-C3 via an HTTP GET request, it prints: Waiting Data...
⭐ Otherwise, the web application prints: Data received and saved successfully!
localhost/food_irradiation_data_logger/get_data.php
⭐ If the create_table parameter is set as OK, the web application creates the requested database table (entries) and prints: Database Table Created Successfully!
💻 On the index.php file:
⭐ The application interface shows created sample names and data record numbers for each class in the MySQL database.
⭐ If the user clicks the Create Samples submit button on the HTML form, the web application generates CSV files (samples) for Edge Impulse, depending on the selected data type (training or testing).
⭐ If the user clicks the Download button, the application downloads all generated CSV files (samples) in the ZIP file format (data.zip).
Before proceeding with the following steps, I needed to set up Beetle ESP32-C3 on the Arduino IDE and install the required libraries for this project.
If your computer cannot recognize Beetle ESP32-C3 when plugged in via a USB cable, connect Pin 9 to GND (pull-down) and try again.
#️⃣ To add the ESP32-C3 board package to the Arduino IDE, navigate to File ➡ Preferences and paste the URL below under Additional Boards Manager URLs.
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
#️⃣ Then, to install the required core, navigate to Tools ➡ Board ➡ Boards Manager and search for esp32.
#️⃣ After installing the core, navigate to Tools > Board > ESP32 Arduino and select ESP32C3 Dev Module.
#️⃣ To print data on the serial monitor, enable USB CDC On Boot after setting Beetle ESP32-C3.
#️⃣ Finally, download the required libraries for the Geiger counter module, the I2C HX711 weight sensor, the AS7341 visible light sensor, and the SSD1309 OLED transparent screen:
DFRobot_Geiger | Download DFRobot_HX711_I2C | Download DFRobot_AS7341 | Download U8g2_Arduino | Download
To display images (monochrome) on the SSD1309 transparent OLED screen successfully, I needed to convert PNG or JPG files into the XBM (X Bitmap Graphic) file format.
#️⃣ First of all, download GIMP.
#️⃣ Then, upload an image (black and white) and go to Image ➡ Scale Image... to resize the uploaded image.
#️⃣ Go to Image ➡ Mode and select Grayscale.
#️⃣ Finally, export the image as an XBM file.
#️⃣ After exporting the image, add the generated data array to the code and print it on the screen.
After setting up Beetle ESP32-C3 and installing the required libraries, I programmed Beetle ESP32-C3 to collect ionizing radiation, weight, and visible light (color) measurements in order to store them on the MySQL database and create appropriately formatted samples for Edge Impulse.
CPM (Counts per Minute)
nSv/h (nanoSieverts per hour)
μSv/h (microSieverts per hour)
Weight (g)
F1 (405 - 425 nm)
F2 (435 - 455 nm)
F3 (470 - 490 nm)
F4 (505 - 525 nm)
F5 (545 - 565 nm)
F6 (580 - 600 nm)
F7 (620 - 640 nm)
F8 (670 - 690 nm)
Since I needed to assign food irradiation dose levels (classes) theoretically as labels for each data record while collecting data from foods to create a valid data set, I utilized the control buttons attached to Beetle ESP32-C3 so as to choose among irradiation dose classes. After selecting an irradiation dose class, Beetle ESP32-C3 appends the selected class to the collected data and then transmits that data packet to the web application.
Control Button (A) ➡ Regulated
Control Button (B) ➡ Unsafe
Control Button (C) ➡ Hazardous
You can download the IoT_food_irradiation_data_collect.ino file to try and inspect the code for collecting ionizing radiation, weight, and visible light (color) measurements and for transferring information to a given web application.
⭐ Include the required libraries.
⭐ Define the Wi-Fi network settings and use the WiFiClient class to create TCP connections.
⭐ Define the Geiger counter module.
⭐ Define the I2C HX711 weight sensor.
⭐ Define the AS7341 visible light sensor settings and objects.
⭐ Define the 1.51” SSD1309 OLED transparent display settings.
⭐ Define monochrome graphics. ⭐ Initialize the SSD1309 OLED transparent display.
⭐ In the err_msg function, display the error message on the SSD1309 OLED transparent screen.
⭐ Check the connection status between the weight (HX711) sensor and Beetle ESP32-C3.
⭐ Set the calibration weight (g) and threshold (g) to calibrate the weight sensor automatically.
⭐ Display the current calibration value on the serial monitor.
⭐ Check the connection status between the AS7341 visible light sensor and Beetle ESP32-C3. Then, enable the built-in LED on the AS7341 sensor.
⭐ Initialize the Wi-Fi module.
⭐ Attempt to connect to the given Wi-Fi network.
⭐ In the get_Weight function, obtain the weight (g) measurement generated by the I2C HX711 weight sensor.
⭐ In the get_Visual_Light function, start spectrum measurement with the AS7341 sensor and read the value of sensor data channel 0~5 under these channel mapping modes:
eF1F4ClearNIR
eF5F8ClearNIR
⭐ In the activate_Geiger_counter function:
⭐ Initialize the Geiger counter module and enable the external interrupt.
⭐ Every three seconds, pause the count to turn off the external interrupt trigger.
⭐ Evaluate the current CPM (Counts per Minute) by dropping the edge pulse within three seconds: the error is ±3CPM.
⭐ Obtain the current nSv/h (nanoSieverts per hour).
⭐ Obtain the current μSv/h (microSieverts per hour).
⭐ In the drawNumber function, convert numbers to char arrays with the itoa function so as to display them on the SSD1309 OLED transparent screen.
⭐ In the home_screen function, display the collected data on the SSD1309 OLED transparent screen.
⭐ In the make_a_get_request function:
⭐ Connect to the web application named food_irradiation_data_logger.
⭐ Create the query string with the collected data.
⭐ Make an HTTP GET request with the data parameters to the web application.
⭐ Wait until the client is available, then fetch the response from the web application.
⭐ If there is a response from the server and the web application appends the transferred data packet to the MySQL database successfully, print Data registered successfully! on the serial monitor and the SSD1309 screen.