C4001 Human sensor code examples not working on Raspberry Pi 5 (Python)
Hello I have the C4001 human presence sensor and I am trying to use it on a Raspberry PI. So far the code from github is not working (the examples).
First Question:
The two dip switches on the device, how should they positioned? If I wanted to use I2C how should they be set, if I wanted to use UART how should they be set?
The python code when imported into Thonny throws a bunch of errors, many were due to incompatible line spacing I had to manually fix them.
IF using I2C which GPIO pins should I use on the Raspberry Pi?
IF using UART which GPIO pins should I use?
I noticed in the code when it appears to use I2C it doesn't ask for any GPIO PIN numbers (SDA, CLK) so I'm confused how this is supposed to work. I used CuteCom to connect and I saw some data coming through via serial, it was mostly just:
$DFHPD,0, , , *␍␊
$DFHPD,1, , , *␍␊
Please help and fix the code, I cannot use the product!!
Right now I have the dip switches set to I2C and 0x2B (both switches in the down position). I try and run the motion_dectection.py example provided and I get the follow warnings (no errors technically):
motion_detection.py
Line 14 : Unused import os
It looks like the imported module or variable is not used.
Line 17 : Unused RPi.GPIO imported as GPIO
However in the REPL I get many errors and even more errors listed on “assistant” pane of Thonny for the main DFRobot .py file.
REPL SHELL:
Traceback (most recent call last):
File "/home/fed/Desktop/Motion_Detect/DFRobot_C4001-main/python/raspberrypi/examples/motion_detection.py", line 73, in <module>
loop()
File "/home/fed/Desktop/Motion_Detect/DFRobot_C4001-main/python/raspberrypi/examples/motion_detection.py", line 66, in loop
if 1 == radar.motion_detection():
File "/home/fed/Desktop/Motion_Detect/DFRobot_C4001-main/python/raspberrypi/examples/DFRobot_C4001.py", line 176, in motion_detection
return rslt[0]&0x01
TypeError: 'int' object is not subscriptable
And a ton of issues in the assistant pane:
DFRobot_C4001.py
Line 138 : "DFRobot_C4001" has no attribute "read_reg" [attr-defined]
Line 146 : Either all return statements in a function should return an expression, or none of them should.
According to PEP8, if any return statement returns an expression, any return statements where no value is returned should explicitly state this as return None, and an explicit return statement should be present at the end of the function (if reachable)
Line 152 : "DFRobot_C4001" has no attribute "write_reg" [attr-defined]
Line 155 : "DFRobot_C4001" has no attribute "write_reg" [attr-defined]
Line 158 : "DFRobot_C4001" has no attribute "write_reg" [attr-defined]
Line 161 : "DFRobot_C4001" has no attribute "write_reg" [attr-defined]
Line 164 : "DFRobot_C4001" has no attribute "write_reg" [attr-defined]
Line 167 : "DFRobot_C4001" has no attribute "write_reg" [attr-defined]
Line 175 : "DFRobot_C4001" has no attribute "read_reg" [attr-defined]
Line 178 : "DFRobot_C4001" has no attribute "read_reg" [attr-defined]
Line 182 : Either all return statements in a function should return an expression, or none of them should.
According to PEP8, if any return statement returns an expression, any return statements where no value is returned should explicitly state this as return None, and an explicit return statement should be present at the end of the function (if reachable)
Line 191 : "DFRobot_C4001" has no attribute "write_reg" [attr-defined]
Line 194 : "DFRobot_C4001" has no attribute "write_reg" [attr-defined]
Line 197 : "DFRobot_C4001" has no attribute "write_reg" [attr-defined]
Line 199 : "DFRobot_C4001" has no attribute "write_reg" [attr-defined]
Line 201 : "DFRobot_C4001" has no attribute "write_reg" [attr-defined]
Line 211 : "DFRobot_C4001" has no attribute "write_reg" [attr-defined]
Line 222 : "DFRobot_C4001" has no attribute "read_reg" [attr-defined]
Line 229 : Either all return statements in a function should return an expression, or none of them should.
According to PEP8, if any return statement returns an expression, any return statements where no value is returned should explicitly state this as return None, and an explicit return statement should be present at the end of the function (if reachable)
Line 235 : "DFRobot_C4001" has no attribute "write_reg" [attr-defined]
Line 246 : "DFRobot_C4001" has no attribute "read_reg" [attr-defined]
Line 254 : Either all return statements in a function should return an expression, or none of them should.
According to PEP8, if any return statement returns an expression, any return statements where no value is returned should explicitly state this as return None, and an explicit return statement should be present at the end of the function (if reachable)
Line 264 : "DFRobot_C4001" has no attribute "write_reg" [attr-defined]
Line 276 : "DFRobot_C4001" has no attribute "read_reg" [attr-defined]
Line 285 : "DFRobot_C4001" has no attribute "read_reg" [attr-defined]
Line 305 : "DFRobot_C4001" has no attribute "write_reg" [attr-defined]
Line 320 : "DFRobot_C4001" has no attribute "read_reg" [attr-defined]
Line 330 : "DFRobot_C4001" has no attribute "read_reg" [attr-defined]
Line 339 : "DFRobot_C4001" has no attribute "read_reg" [attr-defined]
Line 348 : "DFRobot_C4001" has no attribute "read_reg" [attr-defined]
Line 373 : "DFRobot_C4001" has no attribute "read_reg" [attr-defined]
Line 386 : Either all return statements in a function should return an expression, or none of them should.
According to PEP8, if any return statement returns an expression, any return statements where no value is returned should explicitly state this as return None, and an explicit return statement should be present at the end of the function (if reachable)
Line 399 : "DFRobot_C4001" has no attribute "write_reg" [attr-defined]
Line 411 : Either all return statements in a function should return an expression, or none of them should.
According to PEP8, if any return statement returns an expression, any return statements where no value is returned should explicitly state this as return None, and an explicit return statement should be present at the end of the function (if reachable)
Line 449 : Incompatible types in assignment (expression has type "float", variable has type "int") [assignment]
Line 450 : Incompatible types in assignment (expression has type "float", variable has type "int") [assignment]
Line 451 : Incompatible types in assignment (expression has type "float", variable has type "int") [assignment]
Line 470 : Either all return statements in a function should return an expression, or none of them should.
According to PEP8, if any return statement returns an expression, any return statements where no value is returned should explicitly state this as return None, and an explicit return statement should be present at the end of the function (if reachable)
Line 472 : "DFRobot_C4001" has no attribute "read_reg" [attr-defined]
Line 478 : Redefining name 'status' from outer scope (line 66)
It looks like the local variable is hiding a global variable with the same name.
Most likely there is nothing wrong with this. I just wanted to remind you that you can't access the global variable like this. If you knew it then please ignore the warning.
If you don't want to see this reminder in the future, then add "redefined-outer-name" (without quotes) into "Tools → Options → Assistant → Disabled checks".
Line 482 : "DFRobot_C4001" has no attribute "write_reg" [attr-defined]
Line 492 : "DFRobot_C4001" has no attribute "read_reg" [attr-defined]
Line 499 : Either all return statements in a function should return an expression, or none of them should.
According to PEP8, if any return statement returns an expression, any return statements where no value is returned should explicitly state this as return None, and an explicit return statement should be present at the end of the function (if reachable)
Line 580 : Unused argument 'count'
If you want to keep the argument and silence this warning, then add comment "#pylint: disable=unused-argument" as first line in your function body.
Line 582 : "DFRobot_C4001" has no attribute "write_reg" [attr-defined]
Line 584 : "DFRobot_C4001" has no attribute "write_reg" [attr-defined]
Line 585 : "DFRobot_C4001" has no attribute "read_reg" [attr-defined]
Line 586 : Unused variable 'len'
Looks like the variable is defined (or imported) but not used.
Line 589 : "DFRobot_C4001" has no attribute "write_reg" [attr-defined]
Line 594 : "DFRobot_C4001" has no attribute "write_reg" [attr-defined]
Line 596 : "DFRobot_C4001" has no attribute "write_reg" [attr-defined]
Line 599 : "DFRobot_C4001" has no attribute "write_reg" [attr-defined]
Line 601 : "DFRobot_C4001" has no attribute "write_reg" [attr-defined]
Line 603 : "DFRobot_C4001" has no attribute "write_reg" [attr-defined]
Line 620 : Either all return statements in a function should return an expression, or none of them should.
According to PEP8, if any return statement returns an expression, any return statements where no value is returned should explicitly state this as return None, and an explicit return statement should be present at the end of the function (if reachable)
Line 629 : Unused private member `DFRobot_C4001_UART.__uart_i2c`
It looks like the private member of a class is defined but not used.
Line 630 : Unused private member `DFRobot_C4001_UART.__Baud`
It looks like the private member of a class is defined but not used.
Line 633 : Unused argument 'reg'
If you want to keep the argument and silence this warning, then add comment "#pylint: disable=unused-argument" as first line in your function body.
Line 643 : Unused argument 'reg'
If you want to keep the argument and silence this warning, then add comment "#pylint: disable=unused-argument" as first line in your function body.
motion_detection.py
Line 14 : Unused import os
It looks like the imported module or variable is not used.
Line 17 : Unused RPi.GPIO imported as GPIO
I think the code was written or is Python2 code. I could be wrong. I say this because I when I had to manually fix the indentation on EVERY SINGLE LINE OF CODE, it only had say 2 blank spaces instead of 4, well modern Thonny (and possibly other IDE's are not liking that). It says it was tested on a Rpi3, I don't know the release dates of the Rpi3 and when Python2 was shown the door officially for Python 3.
@DFROBOT
We just need some help here, the code is likely still valid, I noticed while trying all sorts of things if I made some changes to the ctype being UART_MODE or I2C_MODE in both the code and within the main “driver” file at one point I didn't get any errors but it was just printing everything as zero, distance, speed, ect… 0. So that tells me the code likely needs minor modification.
I'm not super strong in Python to understand the driver file, I dont understand why there are three classes and they are all instantiated when you run the program, you get the main C4001 class, then you get a C4001UART class and a C4001I2C class. This doesnt make sense to me. I would think it would be one class and it would communicate based on what the demo it set to use (I2C_MODE or UART_MODE) though I even got some strange behavior when changing the ctype = UART (or I2C depending on the demo) almost if the condition of elif wasn't working right. I dont understand ctype.
Anyways all this is just random information you don't need to answer it, but you DO need to fix your code. I paid a lot of money for this little sensor and the examples should work, there's nothing different in a Raspberry Pi3 vs a Pi5 from a Python coding level (other than perhaps an end of life version of Python on the RPi 3) that would stop this from working, its not hardware related.
Documentation says 3.3V or 5V I've been using 3.3V but should I try 5V? Still the problem remains of the actual code.
Like I said when hooked up to a terminal the sensor was providing garbled data and I saw a 0 change to a 1 when I moved the sensor so its working, the code to give us the data isn't!
I opened a ticket in GitHub as well. @DFROBOT you products are great and I've been a big fan, please help fix this situation, if you are offering the code, then it needs to work out of the box. I think this is a matter of outdated code and perhaps some other things. I cannot use Arduino because I already have a python program I've written that needs this sensor to be integrated with it. I need the motion detection working. Please help ASAP and keep us posted. If you want to write a Micropython code version for the ESP32 microcontrollers that would be neat too, though not terribly helpful for my situation where the code has to run on the Rpi 5. I have many ESP32's I could at least try the Micropython code on it. But again, there are Pin's that need to be defined for I2C and I saw none of that in your code. I just don't understand how it works.
Thank you kindly @DFROBOT, looking to hear from you soon!!! :)
Federico.Arismendi