element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Low Power IoT Design Challenge
  • Challenges & Projects
  • Design Challenges
  • Low Power IoT Design Challenge
  • More
  • Cancel
Low Power IoT Design Challenge
Blog Object Classifier and Counter #5 - Sorting Objects with the 3D Magnetic Sensor 2Go TLE493D
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: guillengap
  • Date Created: 29 Sep 2021 5:59 PM Date Created
  • Views 1459 views
  • Likes 1 like
  • Comments 0 comments
  • infineon
  • psoc 6 pineer kit
  • low power iot design challenge
Related
Recommended

Object Classifier and Counter #5 - Sorting Objects with the 3D Magnetic Sensor 2Go TLE493D

guillengap
guillengap
29 Sep 2021

Table of Contents

  1. Project Introduction
  2. Getting Started
  3. Controlling a Servo
  4. Controlling a Robot Arm
  5. 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.

image

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.

image

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.

image

In the image below we can see the board GPIO pinout.

image

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

image

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

image

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:

image

How does it works?

  1. 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;
  2. 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;
  3. When a rubber ball hits the plastic strip, the magnetic sensor knob turns and goes into the area of negative radian values;
  4. 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;
  5. 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;
  6. The cycle repeats until there're no more rubber balls; and
  7. This system can be used to detect, count, and sorter objects.

image

 

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.

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

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.
  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube