Infineon / TLE493D-3DMagnetic-Sensor

Library for Infineons's 3D magnetic sensor TLE493D for Arduino.
MIT License
26 stars 14 forks source link

PWM output #32

Closed ozzcet closed 3 months ago

ozzcet commented 4 months ago

Actually I am searching how could set PWM out according to x,y,z direction movement of magnet. I use platform io with XMC 2Go Kit (actually my board contain TLE493D on board. TLE493D-p2b6-2go) after some test I understand that some pin of board (You may see the image) 1,2,3 has pwm out option I mean when ı connect led to number 1 pin ı can control its brightness like below code.

for(int k=255; k >= 0; k--){ analogWrite(pin1, k); delay(5); }

similar this , I want to map x direction magnetic value for controlling led brightness. is this possible such a thing.

Best regards.

9Volts9er commented 3 months ago
  1. Configure the sensor (as basis you can use e.g. the "Cartesian" example)
  2. In the loop: Readout the sensor
  3. Map magnetic field values to PWM output (convert magnetic field values to 0-255 integer values for pwm output)
  4. Write PWM to the pins

I've not tested it, but it should work somehow like this:

#include <Tle493d.h>

#define MAX_MAG_FIELD 50
#define PWM_X_PIN 1
#define PWM_Y_PIN 2
#define PWM_Z_PIN 3

Tle493d Tle493dMagnetic3DSensor = Tle493d();

void setup() {
  Serial.begin(9600);
  while (!Serial);

  //If using the MS2Go-Kit: Enable following lines to switch on the sensor
  // ***
  pinMode(LED2, OUTPUT);
  digitalWrite(LED2, HIGH);
  delay(50);
  // ***

  Tle493dMagnetic3DSensor.begin();
  Tle493dMagnetic3DSensor.enableTemp();
}

void loop() {
  double x,y,z;
  //Read magnetic values into variables
  Tle493dMagnetic3DSensor.updateData();
  x=abs(Tle493dMagnetic3DSensor.getX());
  y=abs(Tle493dMagnetic3DSensor.getY());
  z=abs(Tle493dMagnetic3DSensor.getZ());

  //map magnetic field to pwm value
  int pwm_x = map(x,0,MAX_MAG_FIELD,0,255);
  int pwm_y = map(y,0,MAX_MAG_FIELD,0,255);
  int pwm_z = map(z,0,MAX_MAG_FIELD,0,255);

  //write pwm value to outputs
  analogWrite(PWM_X_PIN, pwm_x);
  analogWrite(PWM_Y_PIN, pwm_y);
  analogWrite(PWM_Z_PIN, pwm_z);

  delay(5);
}
ozzcet commented 3 months ago

Thank You so much my friend.