# DFROBOT GP8302 Javascript function with I2Coutput node

**URL:** <https://discourse.nodered.org/t/dfrobot-gp8302-javascript-function-with-i2coutput-node/87232>\
**Category:** Hardware\
**Created:** [14 April 2024 12:07 UTC](https://discourse.nodered.org/t/dfrobot-gp8302-javascript-function-with-i2coutput-node/87232 "2024-04-14T12:07:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Francois](https://avatars.discourse-cdn.com/v4/letter/f/77aa72/32.png) [@Francois](https://discourse.nodered.org/u/Francois)\
**Post date:** [14 April 2024 12:07 UTC](https://discourse.nodered.org/t/dfrobot-gp8302-javascript-function-with-i2coutput-node/87232/1 "2024-04-14T12:07:37Z")

</div>

```auto
Hi there. If you need to drive a GP8302 (Digital to analog current converter), here is the code i wrote with the help of chagpt after few hours of tries. in a javascript function in the dashboard with I2C nodes.
==============================
var value = msg.payload;
value = Math.max(0, Math.min(0xFFF, value)); // Clamp value between 0 and 0xFFF
var lsb4 = value & 0xF; // Least significant 4 bits
var msb8 = (lsb4 << 4); // Shift the least significant bits to the left to occupy the first 4 bits
var byte1 = (value >> 4) & 0xFF; // Most significant bits
var result = (byte1 << 8) | msb8;
msg.payload = result;
return msg;
=========================
You need to set the I2C Ouput
Adresse 88
Command 2
Bytes 3
I used it with a slider from 0 to 4095 and a 470 ohms resistor wired on the output of the device. The current goes from 0 to 20ma
Hope this useful :slight_smile:

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [13 June 2024 12:07 UTC](https://discourse.nodered.org/t/dfrobot-gp8302-javascript-function-with-i2coutput-node/87232/2 "2024-06-13T12:07:42Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
