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"]]}]

Flow Info

Created 10 years, 6 months ago
Updated 7 years ago
Rating: not yet rated

Owner

Actions

Rate:

Node Types

Core
  • debug (x1)
  • function (x2)
  • inject (x2)
Other
  • gpio out (x2)
  • nodebot (x1)

Tags

  • raspi-io
  • nodebots
  • accelerometer
  • i2c
  • gyro
  • temperature
  • raspberrypi
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option