# Nodered I2C - Raspberry Pi 4 - PM2.5 sensor

**URL:** <https://discourse.nodered.org/t/nodered-i2c-raspberry-pi-4-pm2-5-sensor/95034>\
**Category:** General\
**Tags:** docker\
**Created:** [31 January 2025 15:26 UTC](https://discourse.nodered.org/t/nodered-i2c-raspberry-pi-4-pm2-5-sensor/95034 "2025-01-31T15:26:21Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![John1995](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@John1995](https://discourse.nodered.org/u/John1995)\
**Post date:** [31 January 2025 15:26 UTC](https://discourse.nodered.org/t/nodered-i2c-raspberry-pi-4-pm2-5-sensor/95034/1 "2025-01-31T15:26:22Z")

</div>

Hello everyone,

I launched nodered by means of docker compose up. I connected an air quality sensor to the Raspberry. I installed the i2c packge on Nodered and I connected a timestamp module to the i2c scan node with a debug one at the end. When I click on the timestamp button I find empty into the debug menu. I already checked with the command i2cdetect and these are the results for the i2c channel one.

 ![i2c1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/d/7d0fc5eefe0b08e72cbc6b4876c0b6d36d81fa52.png)

Can anybody help me telling me why I am not seeing sensor's data on nodered? Thanks  
ps. I also tried the chmod 666 /dev/i2c-1, but nothing...

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [31 January 2025 15:58 UTC](https://discourse.nodered.org/t/nodered-i2c-raspberry-pi-4-pm2-5-sensor/95034/2 "2025-01-31T15:58:00Z")

</div>

It is probably because node-red running inside a docker container does not, by default, have access to the hardware.

Unless you have a good reason to run in Docker then don't do that. It just makes life complicated. Use the [pi install script](https://nodered.org/docs/getting-started/raspberrypi) to install it natively.

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [31 January 2025 16:33 UTC](https://discourse.nodered.org/t/nodered-i2c-raspberry-pi-4-pm2-5-sensor/95034/3 "2025-01-31T16:33:01Z")

</div>

> [@Colin](#):
>
> by default, have access to the hardware

that's definitely the issue, [apparently](https://stackoverflow.com/questions/24225647/docker-a-way-to-give-access-to-a-host-usb-or-serial-device) you'll need to do something like:

```auto
docker run -t -i --device=/dev/i2c-1 ...

```

to allow the docker image to access the device.

---

<div class="post-metadata">

**Author:** ![John1995](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@John1995](https://discourse.nodered.org/u/John1995)\
**Post date:** [1 February 2025 11:42 UTC](https://discourse.nodered.org/t/nodered-i2c-raspberry-pi-4-pm2-5-sensor/95034/4 "2025-02-01T11:42:09Z")

</div>

Thanks for the answer.

I will try, but is that a command that I have to use every time that I start the raspberry or not?

Then, is that a command that I have to use in a specific folder, or not?

Thanks

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [1 February 2025 12:19 UTC](https://discourse.nodered.org/t/nodered-i2c-raspberry-pi-4-pm2-5-sensor/95034/5 "2025-02-01T12:19:11Z")

</div>

> [@John1995](#):
>
> I will try, but is that a command that I have to use every time that I start the raspberry or not?

The command you use to start node red in a docker image has to change. If you're using a docker compose file to start node red, then you'll have to make the change there.

It's all about giving docker access to the device and that has to be done using docker.

It's hard to say what exactly has to change since I don't know anything about your setup and how you started node red inside docker.

---

<div class="post-metadata">

**Author:** ![John1995](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@John1995](https://discourse.nodered.org/u/John1995)\
**Post date:** [2 February 2025 13:30 UTC](https://discourse.nodered.org/t/nodered-i2c-raspberry-pi-4-pm2-5-sensor/95034/6 "2025-02-02T13:30:44Z")

</div>

Thanks for the answer.

I used docker compose to run nodered by means of a .yaml file. I always run three containers (one for nodered , one for influxdb and one for grafana).

I think that I have to modify something into the .yaml file, but I don't know what..

Thanks

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [2 February 2025 13:43 UTC](https://discourse.nodered.org/t/nodered-i2c-raspberry-pi-4-pm2-5-sensor/95034/7 "2025-02-02T13:43:13Z")

</div>

> [@John1995](#):
>
> into the .yaml file,

From your description you're using a docker-compose.yaml, then checkout this [post](https://stackoverflow.com/questions/73339141/docker-compose-devices-map-all-devices-from-local-to-container) - you'll need to add the following:

```auto
nodered:
  image: ...nodered...
  devices:
    - /dev/ttyS0:/dev/ttyUSB0
    - /dev/ttyS1:/dev/ttyUSB1
    - /dev/ttyS2:/dev/ttyUSB2
    - /dev/ttyS3:/dev/ttyUSB3

```

modifying the device names accordingly

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [2 February 2025 14:52 UTC](https://discourse.nodered.org/t/nodered-i2c-raspberry-pi-4-pm2-5-sensor/95034/8 "2025-02-02T14:52:35Z")

</div>

> [@John1995](#):
>
> I think that I have to modify something into the .yaml file, but I don't know what..

To reinforce what Colin has already said. Docker adds a significant layer of complexity. Those 3 services are easily directly installed and managed without the need for Docker.

If you are going to use Docker, you will need to do some learning or how to use it. You are already using docker-compose I think which is probably be best approach for a small-ish docker installation. But you do need to learn about the restrictions it brings and how to get around them.

---

<div class="post-metadata">

**Author:** ![John1995](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@John1995](https://discourse.nodered.org/u/John1995)\
**Post date:** [2 February 2025 18:04 UTC](https://discourse.nodered.org/t/nodered-i2c-raspberry-pi-4-pm2-5-sensor/95034/9 "2025-02-02T18:04:08Z")

</div>

> [@gregorius](#):
>
> `device=/dev/i2c-1`

Thanks for the answer.

So in my case, that I have not got USB connections for devices but I2C, have I to put into my .yaml file this?

nodered:  
image: ...nodered...  
devices:  
- /dev/ttyS0:/dev/ttyi2c-1  
- /dev/ttyS1:/dev/ttyi2c-20  
- /dev/ttyS2:/dev/ttyi2c-21

Thanks

---

<div class="post-metadata">

**Author:** ![John1995](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@John1995](https://discourse.nodered.org/u/John1995)\
**Post date:** [2 February 2025 18:05 UTC](https://discourse.nodered.org/t/nodered-i2c-raspberry-pi-4-pm2-5-sensor/95034/10 "2025-02-02T18:05:08Z")

</div>

Yes sure, where can I learn more about it?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [2 February 2025 18:15 UTC](https://discourse.nodered.org/t/nodered-i2c-raspberry-pi-4-pm2-5-sensor/95034/11 "2025-02-02T18:15:38Z")

</div>

> [@John1995](#):
>
> where can I learn more about it?

There are loads of tutorials on the web and there is, of course, decent documentation from Docker.

And, there is always ChatGPT and similar AI tools which may be even more useful, especially if you prefer to explore for yourself.

---

<div class="post-metadata">

**Author:** ![John1995](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@John1995](https://discourse.nodered.org/u/John1995)\
**Post date:** [3 February 2025 10:06 UTC](https://discourse.nodered.org/t/nodered-i2c-raspberry-pi-4-pm2-5-sensor/95034/12 "2025-02-03T10:06:52Z")

</div>

Hello, I have tried to modify the yaml file according to my last message but when I try to launch all the containers with the compose up I receive an error from the nodered container: error gathering device information while adding custom device "/dev/ttyS0": no such file or directory.

Can you help me? Thanks

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [3 February 2025 12:29 UTC](https://discourse.nodered.org/t/nodered-i2c-raspberry-pi-4-pm2-5-sensor/95034/13 "2025-02-03T12:29:29Z")

</div>

> [@John1995](#):
>
> /dev/ttyS0

Those a SERIAL PORTS. I'm assuming that, since your sensor is an I2C device, it is either connected direct to the Pi's GPIO. Therefore not serial.

Personally, I have no idea how to allow a Pi's GPIO to be accessed by Docker as I never use a Pi's GPIO, I always use either an Arduino (connected via USB-Serial) or an ESP (connected via Wi-Fi).

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [4 February 2025 08:46 UTC](https://discourse.nodered.org/t/nodered-i2c-raspberry-pi-4-pm2-5-sensor/95034/14 "2025-02-04T08:46:52Z")

</div>

> [@John1995](#):
>
> nodered:  
> image: ...nodered...  
> devices:
> 
> - /dev/ttyS0:/dev/ttyi2c-1
> - /dev/ttyS1:/dev/ttyi2c-20
> - /dev/ttyS2:/dev/ttyi2c-21

try:

- /dev/ttyi2c-1:/dev/ttyi2c-1

instead

Perhaps also read the [manual](https://docs.docker.com/compose/) on docker compose.

If you are going to use docker compose, then understanding its workings might well be an advantage!

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [4 February 2025 09:33 UTC](https://discourse.nodered.org/t/nodered-i2c-raspberry-pi-4-pm2-5-sensor/95034/15 "2025-02-04T09:33:55Z")

</div>

Can you explain why you need to use Docker, when it is causing such problems?

---

<div class="post-metadata">

**Author:** ![John1995](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@John1995](https://discourse.nodered.org/u/John1995)\
**Post date:** [4 February 2025 09:40 UTC](https://discourse.nodered.org/t/nodered-i2c-raspberry-pi-4-pm2-5-sensor/95034/16 "2025-02-04T09:40:02Z")

</div>

Thanks for the answer. I set .yaml file as suggested by gregorius and now I managed to communicate with the sensor, there are still some problems of data unstability (data changes greatly and quickly) but I think that this problem is not linked to node red or docker. Anyway I am using docker compose because with one command I launch all my containers with all applications, it is more suitable, then the enviroment is divided in single containers and that could be safer, just this..

Another problem is that when I turn off the raspberry and turn on again, the permission for the i2c are not more granted even if I modified the yaml file and the communication is not more granted.. strange

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [4 February 2025 10:38 UTC](https://discourse.nodered.org/t/nodered-i2c-raspberry-pi-4-pm2-5-sensor/95034/17 "2025-02-04T10:38:48Z")

</div>

> [@John1995](#):
>
> raspberry and turn on again, the permission for the i2c

is the device connected? the device permissions are set if the device is connected and otherwise the entry in the /dev directory might not even exist.

---

<div class="post-metadata">

**Author:** ![John1995](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@John1995](https://discourse.nodered.org/u/John1995)\
**Post date:** [4 February 2025 10:45 UTC](https://discourse.nodered.org/t/nodered-i2c-raspberry-pi-4-pm2-5-sensor/95034/18 "2025-02-04T10:45:13Z")

</div>

The only thing that I changed was to supply the sensor with 3.3 V instead of 5V... Now I put again 5 V and now I can't communicate with the sensor again... wow

the yaml file is always the same...

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [4 February 2025 11:02 UTC](https://discourse.nodered.org/t/nodered-i2c-raspberry-pi-4-pm2-5-sensor/95034/19 "2025-02-04T11:02:28Z")

</div>

> [@John1995](#):
>
> with 3.3 V instead of 5V.

hm - my lightbulbs also don't work when I use 120v instead of 240v! 😉

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [4 February 2025 11:05 UTC](https://discourse.nodered.org/t/nodered-i2c-raspberry-pi-4-pm2-5-sensor/95034/20 "2025-02-04T11:05:44Z")

</div>

Try it in a native install of node-red rather than docker to check whether the problem is docker related.

[Next page](https://discourse.nodered.org/t/nodered-i2c-raspberry-pi-4-pm2-5-sensor/95034.md?page=2)
