STM32duino Gauge

by Fixed Until Broken in Circuits > Arduino

436 Views, 2 Favorites, 0 Comments

STM32duino Gauge

DSC04528.JPG

This project is a breadboard design of a CanBus Gauge that uses an STM32 Blue pill, DRV8833 and MCP 2551.

If you prefer videos there will be link at the end!

Supplies

  • 1 STM32 Bluepill
  • 1 MCP 2551 Breakout
  • Breadboards
  • 1 X27.168 Stepper Motor
  • 1 Red LED
  • 1 220ohm resistor
  • ~25 prototyping wires
  • 1 pololu DRV8833 Breakout

Rough Out the Breadboard

DSC04532.JPG

In this step we get all the main components layout on the breadboard. We also get some wiring done. Mostly all the 5V+ lines and Ground lines. We can also start putting in some of the wires to the microcontroller but these may move as we start writing the code.

Code!

Capture.PNG

Below is some barebones code for this to get it to work. I would suggest editing to your needs. Follow along in the code for wiring. for example the sleep pin on the DRV8833 goes to pin PB13 on the STM32.

#include <eXoCAN.h>
#include <Stepper.h>
#include <Adafruit_NeoPixel.h>

// backlight setup
Adafruit_NeoPixel pixels(3, PB4, NEO_GRB + NEO_KHZ800);
char lrgb[3];

// Stepper & erro light setup
int stepsPerRevolution = 635;
int stepperSpeed = 60;
int pos, val, light, motor;

Stepper x27(stepsPerRevolution, PB6, PB7, PB8, PB9); // initialize the stepper library


// Can setup
eXoCAN can(STD_ID_LEN, BR250K, PORTA_11_12_XCVR);
int PodId = 0x003;   // ID for rx filtering   <--- Must be different for each pod
int id, fltIdx;

//Read can Function
inline void canRead(bool print = false) 
{
  if (can.rxMsgLen >= 0)
  {
    if (print)
    {
      if (can.id == PodId)
      {// if statement catches any missed by filter.
      light = (can.rxData.bytes[0]); // first byte is for idiot light
      int m1 = (can.rxData.bytes[1]); // next 3 are added to make the 635 steps of the stepper motor
      int m2 = (can.rxData.bytes[2]);
      int m3 = (can.rxData.bytes[3]);
       motor =   m1 + m2 + m3;
    }
    if(can.id == 0x002){ //0x002 is the backlight
      lrgb[1] = (can.rxData.bytes[0]);
      lrgb[2] = (can.rxData.bytes[1]);
      lrgb[3] = (can.rxData.bytes[2]);
      }
    can.rxMsgLen = -1;
  }
  // return id;
}}

void canISR() // get bus msg frame passed by a filter to FIFO0
{
  can.filterMask16Init(0, PodId, 0x7ff, 0x002, 0x7ff); // supposed to filter messages... always misses the first wrong one
  can.rxMsgLen = can.receive(can.id, fltIdx, can.rxData.bytes); // get CAN msg
  
}

//light function
inline void idiot()
{
 if (light == 1)
 {
  digitalWrite(PB5, HIGH);
  } else {
  digitalWrite(PB5, LOW); 
  }
}

//zero function
inline void x27zero()
{
  x27.setSpeed(stepperSpeed);
  x27.step(-stepsPerRevolution);
  delay(10);
  x27.step(5); //less likely to bottom out now. leaves 595 useable steps but I suggest skipping last 5 also
  pos = 5;  // needs to be the same number as the skipped steps
}

void x27move(unsigned int npos)
{
 if (pos != npos && npos <= stepsPerRevolution){
   val = npos - pos;
   pos = npos;
   x27.step(val);
 }
}

void ws2812(){
  for(int i=0; i<3; i++) { 
    pixels.setPixelColor(i, pixels.Color(lrgb[1], lrgb[2], lrgb[3]));
    pixels.show();   
    }
}
void setup() {
  Serial.begin(112500);
  can.attachInterrupt(canISR);
  pinMode(PB5, OUTPUT); //idiot light pin
  pinMode(PB12, OUTPUT);
  digitalWrite(PB12, HIGH);
  x27zero();
  can.enableInterrupt(); // enable rx interrupt
  pixels.begin();
  pixels.clear();
}

void loop() {
  canRead(true); 
  idiot();
  x27move(motor);
  ws2812();
}<br>

Design a PCB for It!

DSC04529.JPG

Follow the youtube channel Fixed Until Broken to see how I designed this.

Good new is this a very simple to do yourself in Kicad or if you have no experience designing PCBs EasyEDA might be a better fit. If you don't want use the Bluepill and just want to use STM32F103 or you just want to use the dvr8833 and not the module/breakout board than download the schematics for these to use to help you design your board.

Just don't forget to put Pin 3 / pin 6 of your DRV8833 to ground. ( That is the mistake I made in my design). It was easy to overcome my mistake but scraping off some solder mask and running a jump/bodge wire.

Order and Build Your Gauge!

DSC04530.JPG

NextPCB Sponsored this build so I would suggest using them to build your copy of this project. Files are here on github. https://github.com/garnerm91/STM32_pod

⭐Register to get free $100 coupons: https://www.nextpcb.com?code=matt

⭐Double-sided PCB assembly free shipping: https://www.nextpcb.com/pcb-assembly-services?cod...

⭐Free PCB File checker: https://www.nextpcb.com/pcb-assembly-services?cod...

Video!

Open Sourced Universal CAN Bus Gauge Pod Circuit Board