George Kartsonas
Published © GPL3+

3D Sensing for Automotive Control

Electro-mechanical switching is a common source of failure in many applications, including vehicles. 3D magnetic sensing is the solution.

EasyFull instructions provided1 hour2,399

Things used in this project

Hardware components

3D Magnetic Sensor 2Go
Infineon 3D Magnetic Sensor 2Go
×1

Software apps and online services

Arduino IDE
Arduino IDE
Processing

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Code

Arduino Positioning Code

Arduino
#include <Tle493d_w2b6.h>

Tle493d_w2b6 Tle493dMagnetic3DSensor = Tle493d_w2b6(Tle493d_w2b6::LOWPOWERMODE);
void setup() {
  Serial.begin(9600);
  while (!Serial);
  Tle493dMagnetic3DSensor.begin();
  //The highest adjustable range is used for all three directions, i.e., within half of the total value range INT is disabled 
  Tle493dMagnetic3DSensor.setWakeUpThreshold(1,-1,1,-1,1,-1);

  //The update rate is set to 3 (fastest is 0 and slowest is 7)
  Tle493dMagnetic3DSensor.setUpdateRate(3);
  pinMode(14, OUTPUT);
}

void loop() {
  Tle493dMagnetic3DSensor.updateData();

  Serial.print(Tle493dMagnetic3DSensor.getX());
  Serial.print(" ; ");
  Serial.print(Tle493dMagnetic3DSensor.getY());
  Serial.print(" ; ");
  Serial.println(Tle493dMagnetic3DSensor.getZ());

  if (Tle493dMagnetic3DSensor.getX() > -1.0) {
    digitalWrite(14, HIGH);
  } else {
    digitalWrite(14, LOW);
  }
  delay(500);
}

Arduino Processing GUI Code

Arduino
#include <Tle493d_w2b6.h>

void setup() {

  pinMode(14, OUTPUT);   //set pin as output , blue led

  Serial.begin(9600);    //start serial communication @9600 bps
  }

void loop(){
  
  if(Serial.available()){  //id data is available to read

    char val = Serial.read();

     if(val == 'r'){       //if b received
      digitalWrite(14, HIGH); //turn on blue led
      }
   
    if(val == 'f'){       //if f received
      digitalWrite(14, LOW);
      }      
    }
  }

Processing GUI Code

Arduino
import controlP5.*; //import ControlP5 library
import processing.serial.*;

Serial port;

ControlP5 cp5; //create ControlP5 object
PFont font;

void setup(){ //same as arduino program

  size(300, 450);    //window size, (width, height)
  
  printArray(Serial.list());   //prints all available serial ports
  
  port = new Serial(this, "COM18", 9600);  //i have connected arduino to com3, it would be different in linux and mac os
  
  //lets add buton to empty window
  
  cp5 = new ControlP5(this);
  font = createFont("calibri light bold", 26);    // custom fonts for buttons and title
  
  cp5.addButton("red")     //"red" is the name of button
    .setPosition(100, 50)  //x and y coordinates of upper left corner of button
    .setSize(120, 70)      //(width, height)
    .setFont(font)
  ;   
  
  cp5.addButton("alloff")     //"alloff" is the name of button
    .setPosition(100, 250)  //x and y coordinates of upper left corner of button
    .setSize(120, 70)      //(width, height)
    .setFont(font)
  ;
}

void draw(){  //same as loop in arduino

  background(150, 0 , 150); // background color of window (r, g, b) or (0 to 255)
  
  //lets give title to our window
  fill(0, 255, 0);               //text color (r, g, b)
  textFont(font);
  text("LED CONTROL", 80, 30);  // ("text", x coordinate, y coordinat)
}

//lets add some functions to our buttons
//so whe you press any button, it sends perticular char over serial port

void red(){
  port.write('r');
}

void alloff(){
  port.write('f');
}

Credits

George Kartsonas

George Kartsonas

8 projects • 17 followers

Comments