encoder motor module v 1.4 coms issue

:light_bulb: Using this template is recommended — you may also edit it freely, but you must fully describe your product and the problem (at least: product name, version, and problem description).

Product and Purchase

  • Product model: 4-Channel Encoder Motor Driver v1.4
  • SKU or kit name: Hiwonder Ackerman Metal Chassis

Software and Environment

  • Connection method: i2c

Issue Description

  1. What did you expect to happen?
    establish coms with 4-Channel Encoder Motor Driver v1.4 and Arduino r4 wifi
  2. What actually happened?
    i am unable to establish coms , though my project code initially worked but after returning to the project a day later, it completely stopped responding. Given the intermittent history and current failure, I suspect a persistent I2C bus stability issue or a hardware failure on the motor board itself.
  3. Error message or logs: with this basic i2c detection program
    Arduino | How to Scan and Detect I2C Addresses | Adafruit Learning System
    i got “Scanning…No I2C devices found”

Reproduction Steps

7.4V isolated for the steering servo, 12V for the Arduino and motor driver
SCL is connected to A5, SDA to A4, with a shared common ground established across all boards.
The motor board’s status LEDs are active and blinking.
The encoder motors show green blinking lights and exhibit slight physical resistance when powered.
Running a standard I2C scanner attached below returns: “Scanning… No I2C devices found.”

Evidence

  1. Photos or short video:

  2. i understand the mess but have made sure all the jumper wire work they are ok

What You Have Tried

  1. gemini

  2. claude

  3. perplexity

  4. deepseek

  5. my brain

The driver documentation lists 0x34 as the I²C address. On the v1.4 board, a blinking PWR LED is the normal powered-state indicator. The I²C connector’s 5V pin is input-only, so it cannot power the driver board.

Please check that the driver has its own suitable motor supply, that the Arduino and driver share GND, and that SDA, SCL, and GND are connected to the correct I²C pins for your Arduino R4 WiFi. The driver tutorial’s A4/A5 wiring is for an Arduino UNO; verify the R4 pin mapping against its documentation. Then scan again for address 0x34.

If it still finds no device, please post a clear close-up of the driver’s I²C connector and its wiring, plus the measured voltage between the driver’s I²C 5V and GND pins.


If this reply was helpful, you can mark it as the solution using the button below.

The motor board is powered by a 12V buck converter with SCL/SDA correctly wired and a shared ground, yet the code still returns 'no I2C device found

Your latest photo shows an Arduino MEGA 2560 R3, rather than the R4 WiFi mentioned initially. Please confirm which Arduino you are currently testing—the I²C pin choice matters here.

  1. Check the Arduino-side pins. Our driver tutorial’s A4/A5 connection is specifically for the Arduino UNO. For the Mega shown in your latest photo, connect the driver’s SDA and SCL to the Arduino pins labelled SDA and SCL, respectively, with GND connected between both boards. Do not use A4/A5 for this test on the Mega.
  2. Run a minimal connection test. Switch off power before changing wiring. Disconnect the motors and other peripherals, leaving only the separately powered Arduino, the driver’s external power supply, and the SDA/SCL/GND connections. Then power up and run the scanner again. The documented driver address is 0x34.
  3. Measure the supply at the driver’s power input, between its + and - terminals, while it is powered—not only at the buck converter output. The driver guide lists a DC3V–12V supply range, selected to match the motors. A blinking PWR LED is normal for v1.4.

If the scanner still finds nothing, please post the complete scanner sketch, the board selected in Arduino IDE, the measured driver input voltage, and a close-up showing both ends of the SDA/SCL connections.


If this reply was helpful, you can mark it as the solution using the button below.

The R4 is permanently attached on the chasis, so for clarity, I tested it on a Mega instead. Yup, I’ve already followed your wiring instructions. As for the power supply, the input measures 6.3V. Everything is stripped down and benched.

code

// SPDX-FileCopyrightText: 2023 Carter Nelson for Adafruit Industries
//
// SPDX-License-Identifier: MIT
// --------------------------------------
// i2c_scanner
//
// Modified from https://playground.arduino.cc/Main/I2cScanner/
// --------------------------------------

#include <Wire.h>

// Set I2C bus to use: Wire, Wire1, etc.
#define WIRE Wire

void setup() {
  WIRE.begin();

  Serial.begin(9600);
  while (!Serial)
     delay(10);
  Serial.println("\nI2C Scanner");
}


void loop() {
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for(address = 1; address < 127; address++ )
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    WIRE.beginTransmission(address);
    error = WIRE.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");

      nDevices++;
    }
    else if (error==4)
    {
      Serial.print("Unknown error at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.println(address,HEX);
    }
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

  delay(5000);           // wait 5 seconds for next scan
}

result

Thanks for clarifying. Your screenshot shows Arduino Mega or Mega 2560 selected and the Serial Monitor at 9600 baud, matching your sketch. The measured 6.3 V is within the driver’s documented DC3V–12V input range.

Since you have already checked the wiring and stripped the setup down, the next useful tests are:

  1. Compare with the official driver example. Keep the motors disconnected and run the unmodified Arduino example supplied with the 4-Channel Encoder Motor Driver, selecting your Mega board and port. The example reads and prints the driver’s supply voltage over I²C, in addition to sending motor commands. Please post its complete serial output. The documented driver address is 0x34.
  2. If both boards still share the buck-converter supply, test independent power. The driver guide recommends independently powering the controller and driver as one supported arrangement. Keep their grounds connected, and switch power off before changing connections. Then repeat your scanner test.

One correction to my earlier measurement request: the driver guide warns that its I²C 5V pin can show a misleading voltage because of the diode arrangement. Please use the driver’s external power-input terminals for the supply measurement, as you have now done.

If neither test communicates, please send the official example’s output and a fresh close-up of the current bench wiring showing the driver’s connector labels and both Arduino signal connections.


If this reply was helpful, you can mark it as the solution using the button below.

#include <Wire.h>

#define I2C_ADDR 0x34

#define ADC_BAT_ADDR 0x00

#define MOTOR_TYPE_ADDR 0x14 //编码电机类型设置(set encoder motor type)

#define MOTOR_ENCODER_POLARITY_ADDR 0x15 //设置编码方向极性,(set the encoder direction polarity)

//如果发现电机转速根本不受控制,要么最快速度转动,要么停止。可以将此地址的值重新设置一下(If the motor speed is completely uncontrollable, either it rotates at the fastest speed or stops. You can reset the value of this address)

//范围0或1,默认0 (range 0 or 1, default 0)

#define MOTOR_FIXED_PWM_ADDR 0x1F //固定PWM控制,属于开环控制,范围(-100~100)(Fixed PWM control belongs to open loop control. It ranges from -100 to 100)

#define MOTOR_FIXED_SPEED_ADDR 0x33 //固定转速控制,属于闭环控制,(Fixed speed control belongs to closed loop control)

//单位:脉冲数每10毫秒,范围(根据具体的编码电机来,受编码线数,电压大小,负载大小等影响,一般在±50左右)(Unit: pulse count per 10 milliseconds, range (depending on the specific encoder motor, affected by the number of encoding lines, voltage size, load size, etc., generally around ±50))

#define MOTOR_ENCODER_TOTAL_ADDR 0x3C //4个编码电机各自的总脉冲值(total pulse value of 4 encoder motors)

//如果已知电机每转一圈的脉冲数为U,又已知轮子的直径D,那么就可以通过脉冲计数的方式得知每个轮子行进的距离(If the pulse count per revolution of the motor is known as U, and the diameter of the wheel is known as D, then the distance traveled by each wheel can be obtained through pulse counting)

//比如读到电机1的脉冲总数为P,那么行进的距离为(P/U) * (3.14159*D)(For example, if the total pulse count of motor 1 is P, then the distance traveled is (P/U) * (3.14159*D))

//对于不同的电机可以自行测试每圈的脉冲数U,可以手动旋转10圈读出脉冲数,然后取平均值得出(For different motors, you can test the pulse count per revolution U by manually rotating 10 times and reading the pulse count, and then take the average value to get)

//电机类型具体值(motor type specific value)

#define MOTOR_TYPE_WITHOUT_ENCODER 0 //无编码器的电机,磁环每转是44个脉冲减速比:90 默认(Motor without encoder, 44 pulses per magnetic ring rotation, reduction ratio: 90, default)

#define MOTOR_TYPE_TT 1 //TT编码电机(TT encoder motor)

#define MOTOR_TYPE_N20 2 //N20编码电机(N20 encoder motor)

#define MOTOR_TYPE_JGB37_520_12V_110RPM 3 //磁环每转是44个脉冲 减速比:90 默认(44 pulses per magnetic ring rotation, reduction ratio: 90, default)

u8 data[20];//用于暂存I2C读取的数据(used to temporarily store data read by I2C)

uint8_t count = 0;

bool WireWriteByte(uint8_t val)

{

Wire.beginTransmission(I2C_ADDR);

Wire.write(val);

if( Wire.endTransmission() != 0 ) {

    return false;

}

return true;

}

//向地址中写入数据(reg:地址 val:数据内容 len:数据长度)(write data to the address (reg: address val: data content len: data length))

bool WireWriteDataArray( uint8_t reg,uint8_t *val,unsigned int len)

{

unsigned int i;



Wire.beginTransmission(I2C_ADDR);

Wire.write(reg);

for(i = 0; i < len; i++) {

    Wire.write(val\[i\]);

}

if( Wire.endTransmission() != 0 ) {

    return false;

}



return true;

}

//读取地址中的数据(reg:地址 val:数据内容)(read data from the address (reg: address val: data content))

bool WireReadDataByte(uint8_t reg, uint8_t &val)

{

if (!WireWriteByte(reg)) {

    return false;

}



Wire.requestFrom(I2C_ADDR, 1);

while (Wire.available()) {

    val = Wire.read();

}



return true;

}

//读取地址中指定长度的数据(reg:地址 val:数据内容 len:数据长度)(read the data of the specified length from the address (reg: address val: data content len: data length))

int WireReadDataArray( uint8_t reg, uint8_t *val, unsigned int len)

{

unsigned char i = 0;



/\* Indicate which register we want to read from \*/

if (!WireWriteByte(reg)) {

    return -1;

}



/\* Read block data \*/

Wire.requestFrom(I2C_ADDR, len);

while (Wire.available()) {

    if (i >= len) {

        return -1;

    }

    val\[i\] = Wire.read();

    i++;

}



return i;

}

int serial_putc( char c, struct __file * )

{

Serial.write( c );

return c;

}

void printf_begin(void)

{

fdevopen( &serial_putc, 0 );

}

uint8_t MotorType = MOTOR_TYPE_JGB37_520_12V_110RPM; //设定电机类型(set motor type)

uint8_t MotorEncoderPolarity = 0;

void setup()

{

Wire.begin();//初始化I2C,以Ardunio UNO为例I2C口为:A4(SCL)、A5(CLK)(Initialize I2C, for example, the I2C port is A4 (SCL) and A5 (CLK) for Ardunio UNO)

Serial.begin(9600);//初始化串口,波特率设置为9600(Initialize the serial port, and set the baud rate to 9600)

printf_begin();//printf输出初始化(initialize printf output)

delay(200);

WireWriteDataArray(MOTOR_TYPE_ADDR,&MotorType,1);//在电机类型地址中写入电机类型编号(write motor type number in the motor type address)

delay(5);

WireWriteDataArray(MOTOR_ENCODER_POLARITY_ADDR,&MotorEncoderPolarity,1);

}

/*用数组传递电机速度,正数为设置前进速度,负数为设置后退速度(Use an array to pass the motor speed, positive is to set forward speed, negative is to set reverse speed)

以p1、p2为例:p1=4个电机以50的速度前进 p2=4个电机以20的速度后退(Take p1 and p2 as examples: p1 = 4 motors move forward at a speed of 50; p2 = 4 motors move backward at a speed of 20)*/

int8_t p1[4]={50,50,50,50};

int8_t p2[4]={-20,-20,-20,-20};

int8_t stop[4]={0,0,0,0};

int8_t EncodeReset[16]={0};

int32_t EncodeTotal[4]; //用于暂存电机累积转动量的值,正转递增,反转递减(Used to temporarily store the accumulated rotation of the motor, increasing during forward rotation and decreasing during reverse rotation)

void loop()

{

u16 v;//用于暂存电压值(used to temporarily store the voltage value)

WireReadDataArray(ADC_BAT_ADDR,data,2);//读取电压地址中存放的电压(read the voltage stored in the voltage address)

v = data[0]+ (data[1]<<8);

Serial.print("V = ");Serial.print(v);Serial.println("mV ");//将电压转换为mV(convert voltage to mV)

/*在电机转速地址中写入电机的转动方向和速度:WireWriteDataArray(转速控制地址,电机转速数组,电机个数)(Write the motor rotation direction and speed to the motor speed address: WireWriteDataArray (speed control address, motor speed array, and number of motors))*/

WireWriteDataArray(MOTOR_FIXED_SPEED_ADDR,p1,4); //正转(rotate clockwise)

delay(3000);

WireWriteDataArray(MOTOR_FIXED_SPEED_ADDR,p2,4); //反转(rotate counterclockwise)

delay(3000);

//PWM控制,注意:PWM控制是一个持续控制的过程,若有延时则会打断电机的运行(打印也会有一定的延时)(PWM control; note: PWM control is a continuous control process. If there is a delay, it will interrupt the motor operation (there will also be a certain delay in printing))

//WireWriteDataArray(MOTOR_FIXED_PWM_ADDR,s1,4);

WireReadDataArray(MOTOR_ENCODER_TOTAL_ADDR,(uint8_t*)EncodeTotal,16);//读取电机累积转动量(read accumulated motor rotation value)

/*打印4个电机的累计转动量(print the accumulated rotation of the 4 motors)*/

printf(“Encode1 = %ld Encode2 = %ld Encode3 = %ld Encode4 = %ld \r\n”, EncodeTotal[0], EncodeTotal[1], EncodeTotal[2], EncodeTotal[3]);

WireWriteDataArray(MOTOR_FIXED_SPEED_ADDR,stop,4); //停止(stop)

delay(2000);

//清空编码电机的脉冲值,向该地址写入0即可(Clear the pulse value of the encoder motors, and write 0 to the address)

WireWriteDataArray(MOTOR_ENCODER_TOTAL_ADDR,EncodeReset,16);

WireReadDataArray(MOTOR_ENCODER_TOTAL_ADDR,(uint8_t*)EncodeTotal,16);

printf(“Encode1 = %ld Encode2 = %ld Encode3 = %ld Encode4 = %ld \r\n”, EncodeTotal[0], EncodeTotal[1], EncodeTotal[2], EncodeTotal[3]);

}

there is no output from the moter driver , sketch is just returning the template with 0s

Thanks for sharing the sketch and photos. The zero values do not establish that the driver returned valid readings: the sketch prints data and EncodeTotal without checking whether WireReadDataArray() succeeded. With the motors disconnected, zero encoder counts would also be expected.

The scanner previously found no I²C devices, so please focus on that test first:

  1. With power off, use a continuity meter to check each connection end to end: driver SDA → Mega SDA 20, SCL → Mega SCL 21, and driver GND → Mega GND. Check the actual connector contacts as well as the jumper wires.
  2. If you have not already tried it, power the Mega and driver independently, keeping their grounds connected. The driver guide supports independent supplies. Leave the motors disconnected and run the scanner again; the documented driver address is 0x34.
  3. If the scanner still finds nothing, please share the result of the continuity checks and confirm whether the independent-power test changes the result.

For the voltage reading in your example, you can also check the read result before printing it. Replace the voltage-read and print lines in loop() with:

int bytesRead = WireReadDataArray(ADC_BAT_ADDR, data, 2);
if (bytesRead == 2) {
  v = data[0] + (data[1] << 8);
  Serial.print("V = ");
  Serial.print(v);
  Serial.println("mV");
} else {
  Serial.print("Voltage read failed; result = ");
  Serial.println(bytesRead);
}

Please include that result as well. Keep the motors disconnected while testing: the rest of the example sends motor commands.


If this reply was helpful, you can mark it as the solution using the button below.

#include <Wire.h>

#define I2C_ADDR 0x34

#define ADC_BAT_ADDR 0x00

#define MOTOR_TYPE_ADDR 0x14

#define MOTOR_ENCODER_POLARITY_ADDR 0x15

#define MOTOR_FIXED_PWM_ADDR 0x1F

#define MOTOR_FIXED_SPEED_ADDR 0x33

#define MOTOR_ENCODER_TOTAL_ADDR 0x3C

#define MOTOR_TYPE_WITHOUT_ENCODER 0

#define MOTOR_TYPE_TT 1

#define MOTOR_TYPE_N20 2

#define MOTOR_TYPE_JGB37_520_12V_110RPM 3

u8 data[20];

uint8_t count = 0;

bool WireWriteByte(uint8_t val)

{

Wire.beginTransmission(I2C_ADDR);

Wire.write(val);

if( Wire.endTransmission() != 0 ) {

    return false;

}

return true;

}

bool WireWriteDataArray( uint8_t reg, uint8_t *val, unsigned int len)

{

unsigned int i;

Wire.beginTransmission(I2C_ADDR);

Wire.write(reg);

for(i = 0; i < len; i++) {

    Wire.write(val\[i\]);

}

if( Wire.endTransmission() != 0 ) {

    return false;

}

return true;

}

bool WireReadDataByte(uint8_t reg, uint8_t &val)

{

if (!WireWriteByte(reg)) {

    return false;

}

Wire.requestFrom(I2C_ADDR, 1);

while (Wire.available()) {

    val = Wire.read();

}

return true;

}

int WireReadDataArray( uint8_t reg, uint8_t *val, unsigned int len)

{

unsigned char i = 0;

if (!WireWriteByte(reg)) {

    return -1;

}

Wire.requestFrom(I2C_ADDR, len);

while (Wire.available()) {

    if (i >= len) {

        return -1;

    }

    val\[i\] = Wire.read();

    i++;

}

return i;

}

int serial_putc( char c, struct __file * )

{

Serial.write( c );

return c;

}

void printf_begin(void)

{

fdevopen( &serial_putc, 0 );

}

uint8_t MotorType = MOTOR_TYPE_JGB37_520_12V_110RPM;

uint8_t MotorEncoderPolarity = 0;

void setup()

{

Wire.begin();

Serial.begin(9600);

printf_begin();

delay(200);

WireWriteDataArray(MOTOR_TYPE_ADDR, &MotorType, 1);

delay(5);

WireWriteDataArray(MOTOR_ENCODER_POLARITY_ADDR, &MotorEncoderPolarity, 1);

}

int8_t p1[4]={50,50,50,50};

int8_t p2[4]={-20,-20,-20,-20};

int8_t stop[4]={0,0,0,0};

int8_t EncodeReset[16]={0};

int32_t EncodeTotal[4];

void loop()

{

u16 v;

int bytesRead = WireReadDataArray(ADC_BAT_ADDR, data, 2);

if (bytesRead == 2) {

v = data\[0\] + (data\[1\] << 8);

Serial.print("V = ");

Serial.print(v);

Serial.println("mV");

} else {

Serial.print("Voltage read failed; result = ");

Serial.println(bytesRead);

}

WireWriteDataArray(MOTOR_FIXED_SPEED_ADDR, p1, 4);

delay(3000);

WireWriteDataArray(MOTOR_FIXED_SPEED_ADDR, p2, 4);

delay(3000);

WireReadDataArray(MOTOR_ENCODER_TOTAL_ADDR, (uint8_t*)EncodeTotal, 16);

printf(“Encode1 = %ld Encode2 = %ld Encode3 = %ld Encode4 = %ld \r\n”, EncodeTotal[0], EncodeTotal[1], EncodeTotal[2], EncodeTotal[3]);

WireWriteDataArray(MOTOR_FIXED_SPEED_ADDR, stop, 4);

delay(2000);

WireWriteDataArray(MOTOR_ENCODER_TOTAL_ADDR, EncodeReset, 16);

WireReadDataArray(MOTOR_ENCODER_TOTAL_ADDR, (uint8_t*)EncodeTotal, 16);

printf(“Encode1 = %ld Encode2 = %ld Encode3 = %ld Encode4 = %ld \r\n”, EncodeTotal[0], EncodeTotal[1], EncodeTotal[2], EncodeTotal[3]);

}

continuity is all good
i am powering them separately

Thanks for confirming that continuity is good and the boards are powered separately.

The attached screenshot shows the scanner output (Scanning... No I2C devices found), not the output from the revised sketch you posted. That revised sketch should print either V = ...mV or Voltage read failed; result = ....

Please:

  1. Upload the revised sketch to the Mega currently connected to the driver, checking that the selected board and port match that Mega and that the upload completes successfully.
  2. Open Serial Monitor at 9600 baud, reset the Mega, and copy the complete output as text. Keep the motors disconnected because the sketch still sends motor commands.

If it prints Voltage read failed; result = -1, your function is failing when it writes the register address to the driver at 0x34, before requesting the voltage bytes. If it prints 0 or 1, it received fewer than the requested two bytes. Please include that exact result—it will distinguish these cases without repeating the wiring and separate-power checks you have already completed.


If this reply was helpful, you can mark it as the solution using the button below.

it prints out Voltage read failed; result = -1

i am getting

Voltage read failed; result = -1

Encode1 = 0  Encode2 = 0  Encode3 = 0  Encode4 = 0  

Encode1 = 0  Encode2 = 0  Encode3 = 0  Encode4 = 0  

on loop

Thanks—that confirms the transaction fails before any voltage bytes are requested. In your posted sketch, WireReadDataArray() returns -1 when WireWriteByte() fails. The encoder reads also ignore their return values, so failed reads leave the initialized encoder array showing zeros.

The next useful detail is the actual Wire.endTransmission() status, which your function currently converts into false. For this diagnostic test, replace only WireWriteByte() with:

bool WireWriteByte(uint8_t val)
{
  Wire.beginTransmission(I2C_ADDR);
  Wire.write(val);
  uint8_t status = Wire.endTransmission();
  Serial.print("I2C write status = ");
  Serial.println(status);
  return status == 0;
}

Arduino documents these relevant status values:

  • 2: Address was not acknowledged.
  • 3: Transmitted data was not acknowledged.
  • 4: Other transmission error.
  • 5: Timeout. (docs.arduino.cc)

Keep I2C_ADDR at 0x34, as specified in the Hiwonder 4-Channel Motor Driver Module — Arduino Example, §4. Keep the motors disconnected while running the sketch.

Please post the resulting I2C write status = ... line. You have already completed the continuity and independent-power checks; there is no need to repeat those.


If this reply was helpful, you can mark it as the solution using the button below.

I2C write status = 2

Official support staff are being brought in to help with this issue. If you still need the assistant’s help, you can @ me in this topic. @Hiwonder_Scarlett

ok and also why is my post unlisted

Hi,

Thank you for reaching out to us.

Regarding the I2C communication issue you encountered, you may want to take a look at the tutorial for the four-channel motor driver within the small Ackerman chassis documentation. There, you can find the corresponding chassis driving code project for the Arduino Uno development board. Testing with this code might help us determine whether the issue stems from the software code or the hardware itself. You can find the tutorial materials here: Arduino Tutorial-4-CH Motor Driver Version

If you encounter any error messages, please feel free to share them with us. You are also welcome to record a short video so that our engineers can get a clearer view of your setup and provide more targeted advice.

Additionally, regarding your forum post being unlisted, this happened because the post contained too many third-party links. We have reviewed and republished your post.

Thank you for your patience, understanding, and support.

Used your code and modified it a bit scraping the correct configuration

#include <Wire.h>




#define I2C_ADDR        0x34




#define GET_LOW_BYTE(A) (uint8_t)((A))

//宏函数 获得A的低八位

#define GET_HIGH_BYTE(A) (uint8_t)((A) >> 8)

//宏函数 获得A的高八位

#define BYTE_TO_HW(A, B) ((((uint16_t)(A)) << 8) | (uint8_t)(B))

//宏函数 以A为高八位 B为低八位 合并为16位整形




#define ADC_BAT_ADDR                  0

#define MOTOR_TYPE_ADDR               20 //编码电机类型设置

#define MOTOR_ENCODER_POLARITY_ADDR   21 //设置编码方向极性,

#define MOTOR_FIXED_PWM_ADDR      31 //固定PWM控制,属于开环控制,范围(-100~100)

#define MOTOR_FIXED_SPEED_ADDR    51 //固定转速控制,属于闭环控制,

#define MOTOR_ENCODER_TOTAL_ADDR  60 //4个编码电机各自的总脉冲值




//电机类型具体值

#define MOTOR_TYPE_WITHOUT_ENCODER        0

#define MOTOR_TYPE_TT                     1

#define MOTOR_TYPE_N20                    2

#define MOTOR_TYPE_JGB37_520_12V_110RPM   3 //磁环每转是44个脉冲   减速比:90  默认




int servopin=12;    //使用12号口模拟PWM信号

int pulsewidth;     //占空比

void servo(int myangle)

{

  pulsewidth=map(myangle,0,180,500,2500);   //将0°- 180°范围映射到500 - 2500范围(即角度到占空比的转换)

  digitalWrite(servopin,HIGH);    //输出高电平

  delayMicroseconds(pulsewidth);  //模拟占空比输出高电平信号

  digitalWrite(servopin,LOW);     //输出低电平

  delay(20-pulsewidth/1000);    //延时     

}




uint8_t data[20]; // CHANGED: u8 to uint8_t so it compiles

bool WireWriteByte(uint8_t val)   //通过I2C发送字节数据

{

    Wire.beginTransmission(I2C_ADDR);

    Wire.write(val);

    if( Wire.endTransmission() != 0 ) {

        return false;

    }

    return true;

}

bool WireWriteDataArray(  uint8_t reg,uint8_t *val,unsigned int len)    //通过I2C发送数据

{

    unsigned int i;




    Wire.beginTransmission(I2C_ADDR);   //设置任务开始地址

    Wire.write(reg);    //头地址内容写入

    for(i = 0; i < len; i++) {

        Wire.write(val[i]);   //传入的内容写入I2C

    }

    if( Wire.endTransmission() != 0 ) {     //写入是否联通荣判断

        return false;

    }




    return true;

}




uint8_t MotorType = MOTOR_TYPE_JGB37_520_12V_110RPM;    //电机模式设置

uint8_t MotorEncoderPolarity = 0;     //电机极性设置

void setup()

{

  Wire.begin();

  Serial.begin(115200); // ADDED: Serial comms initialization

  pinMode(servopin,OUTPUT);

  delay(200);




  // --- ADDED: Comms Check Block ---

  Serial.println("\n--- Hiwonder I2C Comms Check ---");

  Wire.beginTransmission(I2C_ADDR);

  byte error = Wire.endTransmission();

  if (error == 0) {

    Serial.println("STATUS: Module FOUND at 0x34");

  } else {

    Serial.print("STATUS: FAILED. Error code: ");

    Serial.println(error);

  }

  

  if (WireWriteDataArray(MOTOR_TYPE_ADDR,&MotorType,1)) {

    Serial.println("WRITE TEST: Success (ACK)");

  } else {

    Serial.println("WRITE TEST: Failed (NACK)");

  }

  Serial.println("--------------------------------\n");

  // --------------------------------




  delay(5);

  WireWriteDataArray(MOTOR_ENCODER_POLARITY_ADDR,&MotorEncoderPolarity,1);

  servo(90);




}

int8_t car_forward[4]={-16,0,16,0};   //前进

int8_t car_retreat[4]={16,0,-16,0};   //后退

int8_t car_stop[4]={0,0,0,0};




void loop()

{

  /* 小车前进 */

  WireWriteDataArray(MOTOR_FIXED_SPEED_ADDR,car_forward,4);

  delay(4000);

  WireWriteDataArray(MOTOR_FIXED_SPEED_ADDR,car_stop,4);

  delay(1000);

  /* 小车后退 */

  WireWriteDataArray(MOTOR_FIXED_SPEED_ADDR,car_retreat,4);

  delay(4000);

  WireWriteDataArray(MOTOR_FIXED_SPEED_ADDR,car_stop,4);

  delay(1000);

  servo(45);  //转动到90度    

  /* 小车向左前方行驶,4秒后转向不变,回到起点。 */

  WireWriteDataArray(MOTOR_FIXED_SPEED_ADDR,car_forward,4);

  delay(4000);

  WireWriteDataArray(MOTOR_FIXED_SPEED_ADDR,car_stop,4);

  delay(1000); 

  WireWriteDataArray(MOTOR_FIXED_SPEED_ADDR,car_retreat,4);

  delay(4000);

  servo(90);

  WireWriteDataArray(MOTOR_FIXED_SPEED_ADDR,car_stop,4);

  delay(1000);

  servo(145);

  /* 小车向右前方行驶,4秒后转向不变,回到起点。 */

  WireWriteDataArray(MOTOR_FIXED_SPEED_ADDR,car_forward,4);

  delay(4000);

  WireWriteDataArray(MOTOR_FIXED_SPEED_ADDR,car_stop,4);

  delay(1000);

  WireWriteDataArray(MOTOR_FIXED_SPEED_ADDR,car_retreat,4);

  delay(4000);

  WireWriteDataArray(MOTOR_FIXED_SPEED_ADDR,car_stop,4);

  servo(90);

  delay(1000); 

  

  while(1);

  

}


i got

Hi there,

This is likely an I2C communication issue. Please verify whether the I2C pins are connected correctly:

  • SCL → A5

  • SDA → A4

Please try again and feel free to send us any error messages you encounter. If possible, a short video showing your setup and operation would be even better, as it will help our engineers make a more direct diagnosis and provide you with a more accurate solution.

We look forward to your reply.