Table of Contents
- Project Introduction
- Getting Started
- Controlling a Servo
- Controlling a Robot Arm
- Sorting Objects with the 3D Magnetic Sensor 2Go TLE493D
**********************************************************************************************************************
Sorting Objects with the 3D Magnetic Sensor 2Go TLE493D
In this chapter I'm going to show how to detect objects using the 3D Magnetic Sensor 2Go TLE493D, the robot arm, and the PSoC 62S2 WiFi BT Pioneer Board. In the figure below I show you the electrical diagram used in this chapter.
In the figure below I show you the electrical connection of this system. In the figure we can see the PSoC 62S2 WiFi BT Pioneer board, the six degrees of freedom robot arm, the 3D Magnetic Sensor 2Go TLE493D, the Servo 7, the ULN2803AULN2803A IC, the relay, the wooden platform to slide the rubber balls, and the container.
I had to reviewed the datasheet of my Infineon 3D Magnetic Sensor 2Go. In the image below we can see the components of this board.
In the image below we can see the board GPIO pinout.
To program our board, I had downloaded and installed the next software:
- Arduino IDE
- USB Driver from SEGGER
- Library: TLE493D-3DMagnetic-Sensor
- Board: XMC Microcontroller by Infineon
The latest version of Arduino IDE is downloaded from: https://www.arduino.cc/en/Main/Software
We have to download and install the latest version of SEGGER USB driver so that our device works correctly.
The library TLE493D-3DMagnetic-Sensor is downloaded from: https://github.com/Infineon/TLE493D-W2B6-3DMagnetic-Sensor
The Microcontroller board is downloaded from the official Infineon site on github. There we will find the instructions for a correct installation: https://github.com/Infineon/XMC-for-Arduino
In my case, I installed version 1.4.0. Later, in preferences we added the url of the board and installed it: https://github.com/Infineon/Assets/releases/download/current/package_infineon_index.json
Below you have the code uploaded to the 3D Magnetic Sensor 2Go board: magnetic_sensor.ino
// AUTHOR: GUILLERMO PEREZ GUILLEN
#include <Tle493d.h>
Tle493d Tle493dMagnetic3DSensor = Tle493d();
void setup() {
Serial.begin(9600);
pinMode (3,OUTPUT);
pinMode (14,OUTPUT);
digitalWrite (3,LOW);
digitalWrite (14,HIGH);
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();
}
void loop() {
Tle493dMagnetic3DSensor.updateData();
Serial.print(Tle493dMagnetic3DSensor.getAzimuth());
Serial.print("\t");
if(Tle493dMagnetic3DSensor.getAzimuth()>0) {
digitalWrite (3,LOW);
digitalWrite (14,HIGH);
Serial.println(Tle493dMagnetic3DSensor.getPolar());
}
else {
Serial.println(-Tle493dMagnetic3DSensor.getPolar());
digitalWrite (3,HIGH);
digitalWrite (14,LOW);
delay(1000);
digitalWrite (3,LOW);
digitalWrite (14,HIGH);
}
}
Below is an image of how to mount the 3D Magnetic Sensor 2Go board:
How does it works?
- The magnetic sensor knob can turn and it gives me the Azimuth in radians, to its left it gives positive values and to its right negative values;
- I have attached a plastic strip to the magnetic sensor knob to move it every time the rubber ball hit it or the servo returns it to the to the starting position;
- When a rubber ball hits the plastic strip, the magnetic sensor knob turns and goes into the area of negative radian values;
- Then we activate a pulse through pin 3, and this activates the relay. Finally, this relay activates the user button of the PSoC 62S2 WiFi BT Pioneer board;
- The PSoC 62S2 WiFi BT Pioneer board performs two functions: 1) activate servo 7 to returns the magnetic sensor knob into the area of positive radian values, and 2) the robot arm takes the rubber ball and places it in the container;
- The cycle repeats until there're no more rubber balls; and
- This system can be used to detect, count, and sorter objects.
Below I show you the code uploaded to the PSoC 62S2 WiFi BT Pioneer board: sorting_objects.c
// AUTHOR: GUILLERMO PEREZ GUILLEN
#include "cy_pdl.h"
#include "cy_retarget_io.h"
#include "cyhal.h"
#include "cybsp.h"
/******************************************************************************
* Macros
*****************************************************************************/
#define DELAY_SHORT_MS (250) /* milliseconds */
#define DELAY_LONG_MS (300) /* milliseconds */
#define LED_BLINK_COUNT (4)
#define GPIO_INTERRUPT_PRIORITY (7u)
/* PWM Frequency */
#define PWM_FREQUENCY (50u)
/* PWM Duty-cycle */
#define PWM_DUTY_CYCLE_1 (4.58f) // 30 degrees
#define PWM_DUTY_CYCLE_2 (7.75f) // 90 degrees
#define PWM_DUTY_CYCLE_3 (12.50f) // 180 degrees
#define PWM_DUTY_CYCLE_4 (10.92f) // 150 degrees
#define PWM_DUTY_CYCLE_5 (7.75f) // 90 degrees
#define PWM_DUTY_CYCLE_6 (4.50f) // 30 degrees
#define PWM_DUTY_CYCLE_7 (12.50f) // 30 degrees
/*******************************************************************************
* Function Prototypes
********************************************************************************/
static void gpio_interrupt_handler(void *handler_arg, cyhal_gpio_event_t event);
/*******************************************************************************
* Global Variables
********************************************************************************/
volatile bool gpio_intr_flag = false;
/*******************************************************************************
* Function Name: main
*******************************************************************************/
int main(void)
{
cy_rslt_t result;
uint32_t count = 0;
uint32_t delay_led_blink = DELAY_LONG_MS;
/* PWM object */
cyhal_pwm_t servo_1, servo_2, servo_3, servo_4, servo_5, servo_6, servo_7;
/* Initialize the device and board peripherals */
result = cybsp_init();
/* Board init failed. Stop program execution */
if (result != CY_RSLT_SUCCESS)
{
CY_ASSERT(0);
}
/* Initialize retarget-io to use the debug UART port */
result = cy_retarget_io_init(CYBSP_DEBUG_UART_TX, CYBSP_DEBUG_UART_RX,
CY_RETARGET_IO_BAUDRATE);
/* Initialize the user LED */
result = cyhal_gpio_init(CYBSP_USER_LED, CYHAL_GPIO_DIR_OUTPUT,
CYHAL_GPIO_DRIVE_STRONG, CYBSP_LED_STATE_OFF);
/* Initialize the user button */
result = cyhal_gpio_init(CYBSP_USER_BTN, CYHAL_GPIO_DIR_INPUT,
CYHAL_GPIO_DRIVE_PULLUP, CYBSP_BTN_OFF);
/* Configure GPIO interrupt */
cyhal_gpio_register_callback(CYBSP_USER_BTN,
gpio_interrupt_handler, NULL);
cyhal_gpio_enable_event(CYBSP_USER_BTN, CYHAL_GPIO_IRQ_FALL,
GPIO_INTERRUPT_PRIORITY, true);
/* Enable global interrupts */
__enable_irq();
/* Set the PWM output frequency and duty cycle */
result = cyhal_pwm_set_duty_cycle(&servo_1, PWM_DUTY_CYCLE_1, PWM_FREQUENCY);
result = cyhal_pwm_set_duty_cycle(&servo_2, PWM_DUTY_CYCLE_2, PWM_FREQUENCY);
result = cyhal_pwm_set_duty_cycle(&servo_3, PWM_DUTY_CYCLE_3, PWM_FREQUENCY);
result = cyhal_pwm_set_duty_cycle(&servo_4, PWM_DUTY_CYCLE_4, PWM_FREQUENCY);
result = cyhal_pwm_set_duty_cycle(&servo_5, PWM_DUTY_CYCLE_5, PWM_FREQUENCY);
result = cyhal_pwm_set_duty_cycle(&servo_6, PWM_DUTY_CYCLE_6, PWM_FREQUENCY);
result = cyhal_pwm_set_duty_cycle(&servo_7, PWM_DUTY_CYCLE_7, PWM_FREQUENCY);
/* \x1b[2J\x1b[;H - ANSI ESC sequence for clear screen */
printf("\x1b[2J\x1b[;H");
printf("**************** PSoC 6 MCU: GPIO Interrupt *****************\r\n");
for (;;)
{
/* Start the PWM */
result = cyhal_pwm_start(&servo_1);
cyhal_system_delay_ms(10);
result = cyhal_pwm_start(&servo_2);
cyhal_system_delay_ms(10);
result = cyhal_pwm_start(&servo_3);
cyhal_system_delay_ms(10);
result = cyhal_pwm_start(&servo_4);
cyhal_system_delay_ms(10);
result = cyhal_pwm_start(&servo_5);
cyhal_system_delay_ms(10);
result = cyhal_pwm_start(&servo_6);
cyhal_system_delay_ms(10);
result = cyhal_pwm_start(&servo_7);
cyhal_system_delay_ms(10);
/* Check the interrupt status */
if (true == gpio_intr_flag)
{
gpio_intr_flag = false;
/* Update LED toggle delay */
if (DELAY_LONG_MS == delay_led_blink)
{
delay_led_blink = DELAY_SHORT_MS;
}
else
{
delay_led_blink = DELAY_LONG_MS;
}
}
/* Blink LED four times */
for (count = 0; count < LED_BLINK_COUNT; count++)
{
cyhal_gpio_write(CYBSP_USER_LED, CYBSP_LED_STATE_ON);
cyhal_system_delay_ms(delay_led_blink);
cyhal_gpio_write(CYBSP_USER_LED, CYBSP_LED_STATE_OFF);
cyhal_system_delay_ms(delay_led_blink);
}
cyhal_system_delay_ms(10);
for (int i = 180; i >= 0; i--){ // servo_7
float PWM_DUTY_CYCLE_G = ((i*9.5)/180)+3;
result = cyhal_pwm_set_duty_cycle(&servo_7, PWM_DUTY_CYCLE_G, PWM_FREQUENCY);
result = cyhal_pwm_start(&servo_7);
cyhal_system_delay_ms(20);
}
cyhal_system_delay_ms(100);
for (int i = 0; i <= 190; i++){ // servo_7
float PWM_DUTY_CYCLE_G = ((i*9.5)/180)+3;
result = cyhal_pwm_set_duty_cycle(&servo_7, PWM_DUTY_CYCLE_G, PWM_FREQUENCY);
result = cyhal_pwm_start(&servo_7);
cyhal_system_delay_ms(20);
}
cyhal_system_delay_ms(100);
for (int i = 180; i >= 90; i--){ // servo_3
float PWM_DUTY_CYCLE_C = ((i*9.5)/180)+3;
result = cyhal_pwm_set_duty_cycle(&servo_3, PWM_DUTY_CYCLE_C, PWM_FREQUENCY);
result = cyhal_pwm_start(&servo_3);
cyhal_system_delay_ms(20);
}
cyhal_system_delay_ms(100);
for (int i = 90; i <= 120; i++){ // servo_3
float PWM_DUTY_CYCLE_C = ((i*9.5)/180)+3;
result = cyhal_pwm_set_duty_cycle(&servo_3, PWM_DUTY_CYCLE_C, PWM_FREQUENCY);
result = cyhal_pwm_start(&servo_3);
cyhal_system_delay_ms(20);
}
cyhal_system_delay_ms(100);
for (int i = 110; i >= 60; i--){ // servo_6
float PWM_DUTY_CYCLE_F = ((i*9.5)/180)+3;
result = cyhal_pwm_set_duty_cycle(&servo_6, PWM_DUTY_CYCLE_F, PWM_FREQUENCY);
result = cyhal_pwm_start(&servo_6);
cyhal_system_delay_ms(20);
}
cyhal_system_delay_ms(100);
for (int i = 150; i >= 90; i--){ // servo_4
float PWM_DUTY_CYCLE_D = ((i*9.5)/180)+3;
result = cyhal_pwm_set_duty_cycle(&servo_4, PWM_DUTY_CYCLE_D, PWM_FREQUENCY);
result = cyhal_pwm_start(&servo_4);
cyhal_system_delay_ms(20);
}
cyhal_system_delay_ms(100);
for (int i = 180; i >= 90; i--){ // servo_1 ***
float PWM_DUTY_CYCLE_A = ((i*9.5)/180)+3;
result = cyhal_pwm_set_duty_cycle(&servo_1, PWM_DUTY_CYCLE_A, PWM_FREQUENCY);
result = cyhal_pwm_start(&servo_1);
cyhal_system_delay_ms(20);
}
cyhal_system_delay_ms(100);
for (int i = 90; i <= 120; i++){ // servo_4
float PWM_DUTY_CYCLE_D = ((i*9.5)/180)+3;
result = cyhal_pwm_set_duty_cycle(&servo_4, PWM_DUTY_CYCLE_D, PWM_FREQUENCY);
result = cyhal_pwm_start(&servo_4);
cyhal_system_delay_ms(20);
}
cyhal_system_delay_ms(100);
for (int i = 60; i <= 110; i++){ // servo_6
float PWM_DUTY_CYCLE_F = ((i*9.5)/180)+3;
result = cyhal_pwm_set_duty_cycle(&servo_6, PWM_DUTY_CYCLE_F, PWM_FREQUENCY);
result = cyhal_pwm_start(&servo_6);
cyhal_system_delay_ms(20);
}
cyhal_system_delay_ms(100);
for (int i = 90; i >= 60; i--){ // servo_1 ***
float PWM_DUTY_CYCLE_A = ((i*9.5)/180)+3;
result = cyhal_pwm_set_duty_cycle(&servo_1, PWM_DUTY_CYCLE_A, PWM_FREQUENCY);
result = cyhal_pwm_start(&servo_1);
cyhal_system_delay_ms(20);
}
cyhal_system_delay_ms(100);
for (int i = 120; i <= 150; i++){ // servo_4
float PWM_DUTY_CYCLE_D = ((i*9.5)/180)+3;
result = cyhal_pwm_set_duty_cycle(&servo_4, PWM_DUTY_CYCLE_D, PWM_FREQUENCY);
result = cyhal_pwm_start(&servo_4);
cyhal_system_delay_ms(20);
}
cyhal_system_delay_ms(100);
for (int i = 120; i >= 90; i--){ // servo_3
float PWM_DUTY_CYCLE_C = ((i*9.5)/180)+3;
result = cyhal_pwm_set_duty_cycle(&servo_3, PWM_DUTY_CYCLE_C, PWM_FREQUENCY);
result = cyhal_pwm_start(&servo_3);
cyhal_system_delay_ms(20);
}
cyhal_system_delay_ms(100);
for (int i = 75; i <= 90; i++){ // servo_2
float PWM_DUTY_CYCLE_B = ((i*9.5)/180)+3;
result = cyhal_pwm_set_duty_cycle(&servo_2, PWM_DUTY_CYCLE_B, PWM_FREQUENCY);
result = cyhal_pwm_start(&servo_2);
cyhal_system_delay_ms(20);
}
cyhal_system_delay_ms(100);
for (int i = 110; i >= 30; i--){ // servo_6
float PWM_DUTY_CYCLE_F = ((i*9.5)/180)+3;
result = cyhal_pwm_set_duty_cycle(&servo_6, PWM_DUTY_CYCLE_F, PWM_FREQUENCY);
result = cyhal_pwm_start(&servo_6);
cyhal_system_delay_ms(20);
}
cyhal_system_delay_ms(100);
for (int i = 60; i >= 30; i--){ // servo_1 ***
float PWM_DUTY_CYCLE_A = ((i*9.5)/180)+3;
result = cyhal_pwm_set_duty_cycle(&servo_1, PWM_DUTY_CYCLE_A, PWM_FREQUENCY);
result = cyhal_pwm_start(&servo_1);
cyhal_system_delay_ms(20);
}
cyhal_system_delay_ms(100);
for (int i = 90; i <= 180; i++){ // servo_3
float PWM_DUTY_CYCLE_C = ((i*9.5)/180)+3;
result = cyhal_pwm_set_duty_cycle(&servo_3, PWM_DUTY_CYCLE_C, PWM_FREQUENCY);
result = cyhal_pwm_start(&servo_3);
cyhal_system_delay_ms(20);
}
cyhal_system_delay_ms(10);
result = cyhal_pwm_stop(&servo_1);
result = cyhal_pwm_stop(&servo_2);
result = cyhal_pwm_stop(&servo_3);
result = cyhal_pwm_stop(&servo_4);
result = cyhal_pwm_stop(&servo_5);
result = cyhal_pwm_stop(&servo_6);
result = cyhal_pwm_stop(&servo_7);
cyhal_system_delay_ms(10);
/* Enter deep sleep mode */
cyhal_system_deepsleep();
}
}
/*******************************************************************************
* Function Name: gpio_interrupt_handler
*******************************************************************************/
static void gpio_interrupt_handler(void *handler_arg, cyhal_gpio_irq_event_t event)
{
gpio_intr_flag = true;
}
In the video below I show you a test with the 3D Magnetic Sensor 2Go TLE493D, the robot arm, and the PSoC 62S2 WiFi BT Pioneer Board.
Conclusion:
- In a creative way, here I have used the 3D Magnetic Sensor 2Go TLE493D to detect the rubber balls and warn the PSoC 6 WiFi-BT Pioneer Kit board that it has to activate the robot arm to take the ball and place it in a container. The robot arm goes into low energy mode once this task is done.
- In addition, a servo is activated to return the magnetic sensor to its original position.
- I attach the code used in my Github repository:
- Challenges: Many things can be done to monitor this system. Eg, connect it via Bluetooth to a mobile application, or connect it to an IoT server.







