Skip to content

mingyo186/mcp3008_adc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mcp3008_adc

ROS2 Jazzy driver for MCP3008 10-bit 8-channel ADC over SPI (Microchip).

The MCP3008 is one of the most popular ADCs for Raspberry Pi projects. It provides 8 single-ended analog input channels via SPI, making it easy to read analog sensors (potentiometers, thermistors, photoresistors, etc.) with a Raspberry Pi that has no built-in ADC.

Features

  • Publishes std_msgs/Float32 per active channel on adc/channel{0-7}
  • fake_mode for testing without hardware (random Gaussian data)
  • 8 single-ended channels, 10-bit resolution (1024 steps)
  • SPI interface (no I2C address conflicts)
  • Configurable reference voltage and SPI speed
  • Voltage offset calibration via service call
  • Runtime publish_rate change via ros2 param set

Prerequisites

  • ROS 2 Jazzy
  • Python 3
  • spidev (pip install spidev) — only needed for real hardware
  • SPI enabled on Raspberry Pi (sudo raspi-config)

Installation

cd ~/ros2_ws
colcon build --packages-select mcp3008_adc --symlink-install
source install/setup.bash

Usage

Launch (default: fake_mode)

ros2 launch mcp3008_adc mcp3008_launch.py

Direct execution

ros2 run mcp3008_adc mcp3008_node.py --ros-args -p fake_mode:=true

Real hardware (Raspberry Pi)

ros2 launch mcp3008_adc mcp3008_launch.py params_file:=my_params.yaml

YAML override

mcp3008_adc_node:
  ros__parameters:
    fake_mode: false
    spi_bus: 0
    spi_device: 0
    vref: 3.3
    active_channels: [0, 1, 2, 3]

Topic echo

ros2 topic echo /adc/channel0

Parameters

Parameter Type Default Description
fake_mode bool true Use fake driver (no hardware)
spi_bus int 0 SPI bus number (0 or 1)
spi_device int 0 CE pin (0=CE0, 1=CE1)
spi_speed int 1000000 SPI clock speed (Hz)
vref double 3.3 Reference voltage (Volts)
publish_rate double 10.0 Publishing rate (Hz)
active_channels int[] [0..7] Channels to read (0–7)

Services

Service Type Description
adc/calibrate std_srvs/srv/Trigger Collect samples for 2s, compute voltage offset bias
adc/reset std_srvs/srv/Trigger Clear bias, reinitialise sensor

Package Structure

mcp3008_adc/
├── CMakeLists.txt
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── .gitignore
├── package.xml
├── config/
│   └── mcp3008_params.yaml
├── launch/
│   └── mcp3008_launch.py
├── nodes/
│   └── mcp3008_node.py
├── mcp3008_adc/
│   ├── __init__.py
│   └── mcp3008_driver.py
└── test/
    └── test_mcp3008_node.py

Test Results

Tested on Ubuntu 24.04 (WSL2) with fake_mode: true.

Test Category Test Result
Topics adc/channel{0-7} publishes std_msgs/Float32 PASS
Services adc/calibrate returns success=True PASS
Services adc/reset returns success=True PASS
Parameters publish_rate runtime change PASS
Shutdown Clean exit PASS
Linting pep257, flake8, copyright, xmllint PASS

License

MIT

About

ROS2 Jazzy driver for MCP3008 10-bit 8-channel ADC (SPI). Most popular RPi ADC, configurable Vref.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors