Pic to esp32

Thread Starter

geoffers

Joined Oct 25, 2010
495
Hi all,

Not been on here for a while! I've a question/would like some opinions on learning to use esp32.

I've been programming (not professionally) 8bit pic for quite a while now (25years on and off I reckon) and recently been fairly successful (hence lack of visits here)

I code in mpasm relocatable and last few years non blocking code, gave me headaches to start with!

I've a project coming up where I want to read a flowmeter via rs485 (modbus I think but haven't got the meter yet, AliExpress special)
I also want to read the forward speed of a tractor via can bus.
This is to give a drive faster/slower indication for spreading slurry with a pumped umbilical system.

Rs 485 and modbus I have already used on a pic, can bus I haven't.

The esp32 has can and rs485 on it and the options for some nice displays.

How much of a learning curve is involved for a dinosaur like me to go from my current experience to programming esp32 in c (I assume via Arduino ide would be best?)

Can anyone recommend any good books on c for such applications.

As I write this I'm more inclined towards digging out a pic16 and using a simple LCD display and keypad.

Advice appreciated.
Cheers Geoff
 

nsaspook

Joined Aug 27, 2009
16,309

panic mode

Joined Oct 10, 2011
4,973
do you really need CAN bus? just saying there are other ways to get speed.


for example using GPS one can get speed, location and much more... plus it is completely vehicle agnostic so can be tested by walking, riding bike, driving car...

Code:
#include <TinyGPS++.h>
#include <HardwareSerial.h>

TinyGPSPlus gps;
HardwareSerial ss(2); // Use UART2

void setup() {
  Serial.begin(115200);
  ss.begin(9600, SERIAL_8N1, 16, 17); // TX, RX
}

void loop() {
  while (ss.available() > 0) {
    if (gps.encode(ss.read())) {
      if (gps.speed.isValid()) {
        Serial.print("Speed: ");
        Serial.print(gps.speed.kmph());
        Serial.println(" km/h");
      }
    }
  }
}
CAN bus example:
Code:
#include "driver/twai.h"

// Define CAN Pins
#define CAN_RX GPIO_NUM_4
#define CAN_TX GPIO_NUM_5

void setup() {
  Serial.begin(115200);

  // Initialize configuration structures
  twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(CAN_RX, CAN_TX, TWAI_MODE_LISTEN_ONLY);
  twai_timing_config_t t_config = TWAI_TIMING_CONFIG_250KBITS(); // Tractor standard
  twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();

  // Install and start CAN driver
  if (twai_driver_install(&g_config, &t_config, &f_config) == ESP_OK) {
    Serial.println("Driver installed");
  } else {
    Serial.println("Failed to install");
    return;
  }
 
  if (twai_start() == ESP_OK) {
    Serial.println("Driver started");
  } else {
    Serial.println("Failed to start");
  }
}

void loop() {
  twai_message_t message;
  if (twai_receive(&message, pdMS_TO_TICKS(1000)) == ESP_OK) {
    // Check if the message is PGN 65265 (Hex: FF11)
    if (message.identifier == 0x18FEEE00) { // Example for Cruise Control/Vehicle Speed
      // Data byte 1 & 2 contain speed (varies by specific tractor model)
      float speed = ((message.data[1] << 8) | message.data[0]) * 0.00390625; // Example scaling
      Serial.print("Speed (km/h): ");
      Serial.println(speed);
    }
  }
}
 

panic mode

Joined Oct 10, 2011
4,973
for learning, just talk to google AI for example:
"how to display tractor forward speed using esp32 and arm of a servo"
"how to read tractor speed using esp32 and can bus"
"what esp32, display etc i need to do ...xyz?"

get the parts connect them and if you don't have it working in a day or so something is very wrong.
 

Thread Starter

geoffers

Joined Oct 25, 2010
495
I think you maybe right about learning c on pic first. Every time I think about doing i decide it will be quicker in asm as I know what I'm doing.

How easy is it do non blocking code in c? I have got a bit out of touch with stuff since had kids!!

There is already a socket in the cab of the tractor with a speed output I believe over CAN bus, I get what your saying about other ways to get speed but I think it would be cleaner to use that if I can. Has a 12volt supply too.

I'll confess years ago I bought a book on coding in c++ to write computer code but got a bit bogged down trying it. Maybe it was a bad book but I've found python and Java easier for small programmes on pc.
 

Irving

Joined Jan 30, 2016
5,109
How easy is it do non blocking code in c? I have got a bit out of touch with stuff since had kids!!
Very easy, depending as always on the detail. ESP32 comes loaded with FreeRTOS so creating task-based parallel processes, etc is pretty straightforward. The application you're mooting is easily within an ESP32 dual-core's capability.
 

Thread Starter

geoffers

Joined Oct 25, 2010
495
I think I posted too soon!
1000015750.jpg
This is the pin out for the connection in the cab, I don't know why I assumed it was attached to the canbus, maybe because the others are!
This should be easier to interface to with a pic than going via can.

I have radar corrected speed on the tractor so should be quite accurate.
 

nsaspook

Joined Aug 27, 2009
16,309
I think you maybe right about learning c on pic first. Every time I think about doing i decide it will be quicker in asm as I know what I'm doing.

How easy is it do non blocking code in c? I have got a bit out of touch with stuff since had kids!!

There is already a socket in the cab of the tractor with a speed output I believe over CAN bus, I get what your saying about other ways to get speed but I think it would be cleaner to use that if I can. Has a 12volt supply too.

I'll confess years ago I bought a book on coding in c++ to write computer code but got a bit bogged down trying it. Maybe it was a bad book but I've found python and Java easier for small programmes on pc.
If you want to learn bare-metal C programming on a controller, start with something simple like a 8-bit controller. Jumping into ESP32 API level C programming is pretty much asking to race a car with just a learner's permit. ESP's are not intended to be used "bare-metal", the detailed low-level documentation is poor or missing so most use the ESP-IDF to build on the RTOS framework. If you need Wi-Fi and Bluetooth they are great, if you don't, there are tons of better controllers to learn on.
https://docs.espressif.com/projects/esp-idf/en/stable/esp32s3/api-guides/hardware-abstraction.html

https://www.paaet.edu.kw/media/bk2o0hww/analysis-of-free-rtos-vs-bare-metal-using-esp32.pdf
 

John P

Joined Oct 14, 2008
2,060
If you're a beginner, start with an Arduino. It's been very cleverly designed to be easy for a person with limited skills to connect to a computer and get something running quickly. And users have been very helpful, by creating libraries to do all kinds of tasks (CAN bus and RS485 definitely included) which you can get for free and easily install. No special equipment needed, just connect it to your computer with a USB cable, and that provides power, gives you a programming interface and when the program runs, allows you to communicate with it.
 

panic mode

Joined Oct 10, 2011
4,973
How easy is it do non blocking code in c?
create state machine and use as many states as needed...

example using enum:
Code:
typedef enum { STATE_IDLE, STATE_RUNNING, STATE_ERROR } State_t;
State_t currentState = STATE_IDLE;

void update() {
    switch (currentState) {
        case STATE_IDLE:
            if (input_received) currentState = STATE_RUNNING;
            break;
        case STATE_RUNNING:
            // logic...
            if (error_occurred) currentState = STATE_ERROR;
            break;
        // ...
    }
}
 

Thread Starter

geoffers

Joined Oct 25, 2010
495
IMG_20260515_111556_371.jpg

Well the flow meter has arrived and works .
It's from Ali express so documentation is sparse, looks like it communicates over rs485 modbus rtu protocol?
My rs485>usb is gone wrong so will have wait a bit try and talk to it.
Definitely using a pic as I've already written code for modbus rtu on another project.

Any suggestions for a good book on embedded c? I use mplab x, I think it's the xc8 compiler I would need to use.

I assume I can write some parts in asm and some in c if I go that way.

It would seem make sense use c for the arithmetic section, that normally gets fairly involved in asm.

What is like to end up with is a display in cab to show me flow rate and target speed for a given application rate. Then LEDs to tell me to go faster or slower. The flow rate varied by 40% in one field on Friday from top to bottom which surprised me a bit.

Cheers Geoff
 

nsaspook

Joined Aug 27, 2009
16,309
View attachment 367376

Well the flow meter has arrived and works .
It's from Ali express so documentation is sparse, looks like it communicates over rs485 modbus rtu protocol?
My rs485>usb is gone wrong so will have wait a bit try and talk to it.
Definitely using a pic as I've already written code for modbus rtu on another project.

Any suggestions for a good book on embedded c? I use mplab x, I think it's the xc8 compiler I would need to use.

I assume I can write some parts in asm and some in c if I go that way.

It would seem make sense use c for the arithmetic section, that normally gets fairly involved in asm.

What is like to end up with is a display in cab to show me flow rate and target speed for a given application rate. Then LEDs to tell me to go faster or slower. The flow rate varied by 40% in one field on Friday from top to bottom which surprised me a bit.

Cheers Geoff
Great. There's no real need to mix asm with C for this sort of program. A simple FSM (communication and data extraction/formatting from the returned sensor data) for the modbus connection and a main calculation and display routine for the display is simple in C. If you need help with XC8, just ask.

My bmc_slave code has several examples of MODBUS sensor routines for the Q84 pic controller.
Clone (with git) to a local directory.
https://github.com/nsaspook/mqtt_comedi/tree/opifixes


https://github.com/nsaspook/mqtt_comedi/tree/opifixes/Q84

Doxygen files: https://github.com/nsaspook/mqtt_comedi/tree/opifixes/Q84/html
Screenshot 2026-05-18 at 11-02-19 My Project bmc_slave.X_pzem_rtu.c File Reference.png
Screenshot 2026-05-18 at 11-00-10 My Project bmc_slave.X_pzem_rtu.c Source File.png
 

Thread Starter

geoffers

Joined Oct 25, 2010
495
Thanks, I'll see where this goes, have just downloaded the latest mplabx and xc8 to my new pc.

Used real term and rs485 adapter to confirm it talks in modbus rtu, I'm just waiting on the vendor to send me a data sheet for the modbus registers. I can pick out the file for total measured easy enough. If I don't hear anything I'll have to pump some slurry through and find the flow rate file that way.
It's a glamorous old job
 

Sensacell

Joined Jun 19, 2012
3,784
As a 64 year old geezer that has been coding and building hardware my whole life:

I am currently working on my first big AI coding project, what I can tell you is this:

Now I can spend my mental bandwidth on something productive and useful, rather than grinding through code. I use my smarts now on the BIG PICTURE- gone are the days where I would accept sketchy compromises - simply because I burned out.

The darkest era was the 2010's - this is where the expectations of code went through the roof, while the tools to accomplish the work barely kept pace. At this time I truly felt despair, It just seemed that the job got harder every day, and I saw no solution on the horizon. With AI, it's actually fun to do this stuff again, all my experience comes into play when I write the specifications, and when I debug things, I don't feel that AI threatens me- it gives me wings.


Install Claude Code and give it a whirl.
 

nsaspook

Joined Aug 27, 2009
16,309
Don't use AI to learn anything. Use if after you're an expert. Coding was always the easy part (I still love building things from the bolts up, because that's where you have the time to think about practical programming design), the BIG PICTURE is computer programming, not coding.
 
Top