Skip to content

Instantly share code, notes, and snippets.

@monteslu
Created March 30, 2015 19:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monteslu/f8c7a94454f79d3cfdc5 to your computer and use it in GitHub Desktop.
Save monteslu/f8c7a94454f79d3cfdc5 to your computer and use it in GitHub Desktop.
Read an i2c-based mpu6050 accelerometer on a raspberry pi

Read accelerometer, gyro, and temperature data

One inject writes data to the i2c address and register of the mpu6050 device to configure it for reading.

The top inject button then can request 14 bytes from the same i2c address and formats the response data for display to the debugger.

[{"id":"12867341.ed798d","type":"nodebot","name":"","username":"","password":"","boardType":"raspi-io","serialportName":"","connectionType":"local","mqttServer":"","pubTopic":"","subTopic":"","tcpHost":"","tcpPort":"","sparkId":"","sparkToken":"","beanId":"","impId":""},{"id":"a2a45691.5d5ba8","type":"gpio out","name":"","state":"I2C_READ_REQUEST","pin":"","i2cDelay":"0","i2cAddress":"104","i2cRegister":"59","outputs":1,"board":"12867341.ed798d","x":283,"y":694,"z":"fb26e0ad.04d92","wires":[["7058226d.8fa7dc"]]},{"id":"ed1b31d8.12e4d","type":"debug","name":"","active":true,"console":"false","complete":"false","x":500,"y":622,"z":"fb26e0ad.04d92","wires":[]},{"id":"7058226d.8fa7dc","type":"function","name":"process i2c","func":"var data = msg.payload;\nvar int16 = function(high, low) {\n var result = (high << 8) | low;\n // if highest bit is on, it is negative\n return result >> 15 ? ((result ^ 0xFFFF) + 1) * -1 : result;\n};\n\n\nmsg.payload = {\n accelerometer : {\n x: int16(data[0], data[1]),\n y: int16(data[2], data[3]),\n z: int16(data[4], data[5])\n },\n temperature : int16(data[6], data[7]),\n gyro : {\n x: int16(data[8], data[9]),\n y: int16(data[10], data[11]),\n z: int16(data[12], data[13])\n }\n};\n\nreturn msg;","outputs":1,"valid":true,"x":315,"y":548,"z":"fb26e0ad.04d92","wires":[["ed1b31d8.12e4d"]]},{"id":"970b8ad3.68f478","type":"inject","name":"","topic":"","payload":"14","payloadType":"string","repeat":"","crontab":"","once":false,"x":119,"y":552,"z":"fb26e0ad.04d92","wires":[["a2a45691.5d5ba8"]]},{"id":"7a2b3f6.f85d4c","type":"gpio out","name":"","state":"I2C_WRITE_REQUEST","pin":"","i2cDelay":"0","i2cAddress":"104","i2cRegister":"","outputs":0,"board":"12867341.ed798d","x":275,"y":790,"z":"fb26e0ad.04d92","wires":[]},{"id":"bd2a7e35.42d58","type":"inject","name":"","topic":"","payload":"","payloadType":"none","repeat":"","crontab":"","once":false,"x":90,"y":633,"z":"fb26e0ad.04d92","wires":[["c625f941.39da08"]]},{"id":"c625f941.39da08","type":"function","name":"i2c bytes","func":"msg.payload = [0x6B, 0x00];\nreturn msg;","outputs":1,"valid":true,"x":139,"y":709,"z":"fb26e0ad.04d92","wires":[["7a2b3f6.f85d4c"]]}]
@masalinas
Copy link

Thanks for the flow, do you have any code to transform the raw data to m/^2, Celsius and º for the gyroscope??

Regards ...

@shuynh84
Copy link

I keep getting the following error after doing sudo su, then node-red start. Does raspi-io need to be installed within npm install node-red-contrib-gpio foder?

Started Node-RED graphical event wiring tool..
28 Mar 00:20:21 - [info]
Welcome to Node-RED

28 Mar 00:20:21 - [info] Node-RED version: v0.16.2
28 Mar 00:20:21 - [info] Node.js version: v6.10.1
28 Mar 00:20:21 - [info] Linux 4.4.50-v7+ arm LE
28 Mar 00:20:23 - [info] Loading palette nodes
pi : TTY=unknown ; PWD=/home/pi ; USER=root ; COMMAND=/usr/bin/python -u /usr/lib/node_modules/node-red/nodes/core/hardware/nrgpio.py info
pam_unix(sudo:session): session opened for user root by (uid=0)
pam_unix(sudo:session): session closed for user root
28 Mar 00:20:30 - [info] Dashboard version 2.3.6 started at /ui
28 Mar 00:20:37 - [info] Settings file : /home/pi/.node-red/settings.js
28 Mar 00:20:37 - [info] User directory : /home/pi/.node-red
28 Mar 00:20:37 - [info] Flows file : /home/pi/.node-red/flows_raspberrypi.json
28 Mar 00:20:37 - [info] Server now running at http://127.0.0.1:1880/
28 Mar 00:20:37 - [info] Starting flows
1490678437632 Device(s) RaspberryPi-IO
1490678437663 Device(s) RaspberryPi-IO
1490678437688 Device(s) RaspberryPi-IO
1490678437713 Device(s) RaspberryPi-IO
28 Mar 00:20:37 - [info] Started flows
28 Mar 00:20:37 - [red] Uncaught Exception:
28 Mar 00:20:37 - Error: EACCES: permission denied, open '/sys/class/leds/led0/trigger'
at Error (native)
at Object.fs.openSync (fs.js:641:18)
at Object.fs.writeFileSync (fs.js:1347:33)
at new LED (/home/pi/.node-red/node_modules/raspi-led/dist/index.js:42:18)
at RaspiIOCore.value (/home/pi/.node-red/node_modules/raspi-io-core/dist/index.js:525:34)
at RaspiIOCore.pinMode (/home/pi/.node-red/node_modules/raspi-io-core/dist/index.js:484:21)
at /home/pi/.node-red/node_modules/raspi-io-core/dist/index.js:408:17
at Array.forEach (native)
at /home/pi/.node-red/node_modules/raspi-io-core/dist/index.js:337:32
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickDomainCallback (internal/process/next_tick.js:128:9)
nodered.service: main process exited, code=exited, status=1/FAILURE
Unit nodered.service entered failed state.
nodered.service holdoff time over, scheduling restart.
Stopping Node-RED graphical event wiring tool....
Starting Node-RED graphical event wiring tool....
Started Node-RED graphical event wiring tool..
28 Mar 00:20:40 - [info]

@fabikm
Copy link

fabikm commented Mar 8, 2021

Dear,

Does anybody know where should be the problem with the error message:

Flows stopped due to missing node types.

nodebot

Thank you

@questrov
Copy link

same here, nodebot missing which seems to be the i2c IO, see pic
i2c_IO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment