Control a Huawei R4850G2 power supply over CAN with an ESP32-C3 and MCP2515
The Huawei R4850G2 is a 48 V telecom rectifier that is cheap on the second-hand market and, with the right control, makes a very capable battery charger. Out of the box it sits at a fixed output voltage. To change the voltage or the current limit you have to talk to it over CAN.
This project is a small standalone controller: an ESP32-C3 SuperMini, an MCP2515 CAN module, a 0.91" OLED and a rotary encoder. No PC needed. You turn the knob, set the voltage and current, and the supply follows.
What it does
- Sets the output voltage and current limit with a rotary encoder
- Short press switches between voltage and current editing
- Long press saves the values to the ESP32 flash and to the PSU (as its offline values), so they load at next boot
- The idle screen shows the set voltage and current in large digits; touching the encoder brings up the edit screen
- A small blinking NC on the display means no CAN frames are being received
I wanted the PSU’s non-volatile settings written only on an explicit long press, never automatically, to avoid wearing out its memory. If there is no CAN communication (NC), the long press skips writing to the PSU and only saves to the ESP32 flash, so no frames stay queued in the controller and get sent later. The display shows SAVED when the PSU got the values and NO PSU when it did not.
Parts
| Part | Notes |
|---|---|
| Huawei R4850G2 | 48 V rectifier |
| ESP32-C3 SuperMini | controller |
| MCP2515 CAN module | 8 MHz crystal |
| 0.91" OLED, 128x32 | I2C |
| Rotary encoder with button |
Wiring
| Function | ESP32-C3 SuperMini pin |
|---|---|
| MCP2515 CS | GPIO7 |
| MCP2515 SCK | GPIO4 |
| MCP2515 MISO | GPIO5 |
| MCP2515 MOSI | GPIO6 |
| OLED SDA | GPIO8 |
| OLED SCL | GPIO9 |
| Encoder CLK | GPIO2 |
| Encoder DT | GPIO3 |
| Encoder SW | GPIO0 |
The MCP2515 INT pin is not connected; the code polls instead.
Why MCP2515 and not a SN65HVD230
My first attempt used the ESP32’s built-in CAN controller with an SN65HVD230 transceiver module. The PSU did not react at all and stayed at its default 43.5 V. After testing, the SN65HVD230 modules I had turned out to be defective, so the problem was the hardware, not the code. The test sketch, a checklist for testing a CAN transceiver module and what I found are in a separate article: ESP32-C3 and SN65HVD230 CAN module.
Moving to an MCP2515 module (SPI CAN controller with its own transceiver) fixed it: the ESP32 talked to the PSU, the voltage followed the encoder, and the current limit held while charging a battery. If your PSU does not respond, test the CAN hardware first before debugging code.
CAN protocol (Huawei R4850G2)
The R4850G2 talks on a 29-bit (extended ID) CAN bus at 125 kbit/s. These are the frames this controller uses. Values are 32-bit, big-endian, in bytes 4 to 7.
| Purpose | CAN ID | Data |
|---|---|---|
| Set a value on PSU address 1 | 0x108180FE | 01 CMD 00 00 V3 V2 V1 V0 |
| Same, for all PSUs on the bus | 0x108080FE | same |
| Request data | 0x108040FE | 8 bytes of zero |
| PSU reply (one signal per frame) | 0x1081407F | byte 1 = signal, bytes 4 to 7 = value |
Set-value commands (CMD):
| CMD | Meaning | Raw value |
|---|---|---|
0x00 | Voltage, online (volatile) | volts × 1024 |
0x01 | Voltage, offline (stored in the PSU) | volts × 1024 |
0x03 | Current limit, online (volatile) | amps × 20 |
0x04 | Current limit, offline (stored in the PSU) | amps × 20 |
Worked examples:
- 56.8 V: 56.8 × 1024 = 58163 =
0xE333, so the frame data is01 00 00 00 00 00 E3 33 - 20 A: 20 × 20 = 400 =
0x190, so the frame data is01 03 00 00 00 00 01 90
This sketch does not decode the PSU’s reply. It only uses the arrival of any frame to know that the PSU is alive, which is what clears the NC marker. If the PSU does not react to commands for address 1, try the broadcast ID 0x108080FE.
Online versus offline values. Online values are volatile, so the controller resends them every second together with the data request. The data request is also what makes the PSU answer, so NC reflects a live PSU and not just traffic on the bus. Offline values are the ones the PSU keeps for itself, and they are written only on a long press, to protect its memory.
Beyond this sketch
The PSU can also be switched on and off over CAN, and it reports its measured output. I added both in the later two-supply firmware, where they work on my unit:
| Purpose | CAN ID | Data |
|---|---|---|
| Output ON | 0x108180FE | 01 32 00 00 00 00 00 00 |
| Output OFF | 0x108180FE | 01 32 00 01 00 00 00 00 |
In the replies to the data request, signal 0x75 is the output voltage (value / 1024) and signal 0x81 is the output current (value / 1000). These signal IDs and the scaling come from community documentation; if the readings on your unit look off, compare them with a multimeter.
Code
Written in the Arduino IDE for the ESP32-C3 with three libraries: autowp/arduino-mcp2515, U8g2 for the OLED, and the ESP32 Preferences library for flash storage.
CAN setup is two lines:
mcp.reset();
mcp.setBitrate(CAN_125KBPS, MCP_8MHZ); // 8 MHz crystal on the module
mcp.setNormalMode();
Setting a value is one small function:
const uint32_t ID_SET = 0x108180FE;
enum : uint8_t { CMD_V_ON = 0x00, CMD_V_OFF = 0x01, CMD_I_ON = 0x03, CMD_I_OFF = 0x04 };
bool sendCmd(uint8_t cmd, uint32_t raw) {
can_frame f = {};
f.can_id = ID_SET | CAN_EFF_FLAG;
f.can_dlc = 8;
f.data[0] = 0x01;
f.data[1] = cmd;
f.data[4] = raw >> 24; f.data[5] = raw >> 16;
f.data[6] = raw >> 8; f.data[7] = raw;
return mcp.sendMessage(&f) == MCP2515::ERROR_OK;
}
void sendOnline() {
sendCmd(CMD_V_ON, (uint32_t)(setV * 1024.0f + 0.5f));
sendCmd(CMD_I_ON, (uint32_t)(setI * 20.0f + 0.5f));
}
Design choices worth knowing:
- Limits: voltage 42 to 58.4 V in 0.1 V steps, current 0.5 to 45 A in 0.5 A steps. 58.4 V is 3.65 V per cell on a 16S LiFePO4 pack.
- Encoder: read by polling on the falling edge of CLK with a 35 ms debounce. It is not read in an interrupt, because DT has to be read after the contact has settled.
- NC detection: any CAN frame received resets a 3 second timer; without frames the display shows a blinking NC.
- Boot: the controller loads the saved voltage and current from flash (56.8 V and 20 A the very first time), sends them to the PSU, sends a data request and starts on the large idle screen.
- Flash writes: only on a long press, never automatically.
Repo: github.com/blazeB45/huawei_r4850g2_controller
Later version for two supplies
I later merged this firmware with a second one for the Emerson R48-2000eR into a single program that supports both. It is described in a separate article: One ESP32-C3 controller for Huawei R4850G2 and Emerson R48-2000e rectifiers.
Problems and fixes
- No response from the PSU: defective SN65HVD230 modules, solved by switching to MCP2515. Details and a test checklist in the SN65HVD230 article.
- Encoder reading: reading the encoder by polling with a debounce, instead of in an interrupt, gives clean steps because DT is sampled after the contact settles.
- Stale commands: writing offline values only when CAN communication exists keeps old frames from sitting in the controller’s queue and reaching the PSU later.
Safety
This supply delivers real power at 48 V. When charging a battery, put an ideal diode or a MOSFET on the PSU output so the PSU never sees the battery voltage, set the voltage and current limit to the battery’s specification, and use a BMS. Test first on a resistive load before connecting a battery. At boot the controller sends the last saved voltage and current limit to the PSU, so check the saved values before connecting a battery.