Mercury Sensor

  1. Introduction:

This experiment aims to detect the presence of mercury in an environment using a Mercury Medallion Module connected to a Raspberry Pi Pico microcontroller. Mercury detection is critical in various fields such as environmental monitoring, health, and safety. This setup allows for easy integration and real-time monitoring by providing output through a serial console.





  1. Scope:

This experiment demonstrates a method for interfacing a Raspberry Pi Pico microcontroller with a Mercury Medallion sensor. It covers the basic working principle of mercury detection, hardware and software requirements, the application code for measurement using MicroPython and C, and connection guidelines.



  1. Prerequisite:

3.1. Hardware:

  • Pico microcontroller

  • Mercury Medallion Module

  • Breadboard

  • Jumper wires

  • USB cable (to connect the computer)

3 .2. Software:

  • Thonny Software(For micro python)

  • MicroPython firmware installed on Raspberry Pi Pico

  • ubuntu Terminal(For embedded C)

  • CMake,Pico SDK,GNU Arm Embedded Toolchain,WSL.



  1. Connection Details:

  • VCC of Mercury Medallion Module -> 3.3V pin of the Pico.

  • GND of Mercury Medallion Module -> GND of the Pico.

  • OUT of Mercury Medallion Module -> GPIO 26 (ADC) of the Pico.

This configuration uses GPIO 26 for analog data input, enabling the detection of mercury presence.


UART Serial Console: Use a micro-USB cable to connect the Pico to the PC, then open PuTTY and select the correct COM port and baud rate (115200).



  1. Working Principle:

The Mercury Medallion Module outputs an analog voltage based on mercury concentration. The Raspberry Pi Pico reads this analog signal using its ADC (Analog-to-Digital Converter) on GPIO 26. When the sensor's output voltage exceeds a set threshold, the program registers this as mercury presence and logs the result to the serial console. The threshold value can be modified based on the sensor specifications and sensitivity required

On tilting the module mercury inside small tube connect the switching point and make switch ON while tilting it to other side move the mercury and it disconnects the circuit and switches get OFF. The module operates on 5V DC supply.





  1. Application code:

  • MicroPython:

from machine import ADC, Pin

import time


# Initialize the ADC pin (output connected to GPIO 26)

sensor_pin = ADC(26)


# Set threshold for mercury presence detection

threshold = 500


while True:

# Read analog value from the sensor

mercury_value = sensor_pin.read_u16()

# Check mercury presence based on threshold

if mercury_value > threshold:

print("Mercury Detected!")

else:

print("No Mercury Detected")

# Wait 1 second before the next reading

time.sleep(1)


  • Embedded C:

#include "pico/stdlib.h"

#include "hardware/adc.h"

#include <stdio.h>


#define SENSOR_PIN 26 // GPIO 26


int main() {

stdio_init_all(); // Initialize serial communication

adc_init(); // Initialize ADC

adc_gpio_init(SENSOR_PIN);

adc_select_input(0); // Select ADC0 (GPIO 26)

const uint16_t threshold = 500; // Set threshold for mercury detection


while (true) {

uint16_t mercury_value = adc_read();

if (mercury_value > threshold) {

printf("Mercury Detected!\n");

} else {

printf("No Mercury Detected\n");

}

sleep_ms(1000); // Wait 1 second

}

return 0;

}



  1. Hardware Image:



  1. Output:

The output is displayed in the serial console, which shows "Mercury Detected!" if the sensor reading exceeds the threshold or "No Mercury Detected" otherwise. Adjustments to the threshold allow for different sensitivity levels.

  1. Video Demonstration:





  1. Appendix:

  • Common CMakeLists:

cmake_minimum_required(VERSION 3.12)


# Pull in SDK (must be before project)

include(pico_sdk_import.cmake)


project(pico_experiments C CXX ASM)


set(CMAKE_C_STANDARD 11)

set(CMAKE_CXX_STANDARD 17)


if (PICO_SDK_VERSION_STRING VERSION_LESS "1.3.0")

    message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.3.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")

endif()


# Initialize the SDK

pico_sdk_init()


add_compile_options(-Wall

        -Wno-format          # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int

        -Wno-unused-function # we have some for the docs that arent called

        )

if (CMAKE_C_COMPILER_ID STREQUAL "GNU")

    add_compile_options(-Wno-maybe-uninitialized)

endif()


# Hardware-specific examples in subdirectories:

add_subdirectory(expt_10_LCD)

  • CMakeLists:

cmake_minimum_required(VERSION 3.12)

include(pico_sdk_import.cmake)


project(mercury_detection)

pico_sdk_init()


add_executable(mercury_detection mercury_detection.c)

target_link_libraries(mercury_detection pico_stdlib hardware_adc)

pico_enable_stdio_usb(mercury_detection 1)

pico_enable_stdio_uart(mercury_detection 0)


pico_add_extra_outputs(mercury_detection)

  1. References:

Class Schedules:
# Topic Date & time Action

Curriculum

0% Completed (0/1)