This week I spend most of my time in programming for 3D sensor and Servo control. My development environment is Arduino. You can go to https://github.com/Infineon/XMC-for-Arduino/wiki/Implementation-in-Arduino-IDE for instruction on how to add XMC support for Arduino. After installing Arduino, you need to install 3D sensor library by going to Sketch—> Include Library —> Manage Libraries.. Search for TLV and you should found TLV493D-A1B6 by Infineon. If you cannot find the library. You need to install the Library by download at https://www.arduinolibraries.info/libraries/tlv493-d-a1-b6. Currently, I am using Arduino 1.8.10.
For Hardware prototype, I had been connected 5 LEDS and one Servo (MG996) to these ports
Pin Port In Arduino Program
LED1 P2.15 0
LED2 P2.14 1
LED3 P1.0 2
LED4 P1.1 3
LED5 P1.8 4
MG996 P2.12 5
Note for MG996 servo. We can use 3.3 v for the signal line of the servo. However, if you change servo, you need to check the spec whether they can drive by 3.3 v signal or not.
I had been looking around on the internet for idea on how to program 3D sensor for knob control and I found one which is useful for me to quick start.
It is Multi-Functional Magnetic Knob for Consumer Electronics by Md. Chair Alam at https://www.hackster.io/taifur/multi-functional-magnetic-knob-for-consumer-electronics-006870 The project looks similar I need. So I take his concept and adapt the program to my own.
Concept to check 3D sensor used in the program.
1) Check the Azimuth angle by using getAzimuth() library.
2) We need to multiply by 100 because the value is too small.
3) Get the magnetic field amount by using getAmount() library.
4) When you move knob, compare previous angle with current angle.
o If it’s more than 20 mean move Forward.
o If it’s less than -20 mean move Backward.
5) At the same time compare magnetic field between previous measure and current measure.
o If it’s more than 60 mean Click happen.
We do need to adjust value when we really do the real control panel installation. Now it’s just trial and error for these values.
Currently, Arduino Servo library is not supported for XMC4700. So I need to build simple Servo control by myself by just sending PWM signal with 20 ms period ( Frequency 50 Hz). The duty cycle will control the degree of Servo.
To calculate the duty cycle
1) We need to make sure that the Angle for servo within range 0- 180 degrees by using constrain function.
2) Map the angle to pulse width by Minimum pulse width = 540 and Maximum pulse width = 2000 (trial and error) Because I don’t have information on how duty cycle relates
to servo’s angle at this moments.
3) Send HIGH signal equal to pulse width and LOW signal equal to (20000 (20ms ) - HIGH period ) and loop until the servo reach position.
This is quick and dirty to control Servo. The best way is to use PWM but I don’t know how to set PWM frequency in Arduino with XMC4700 yet. So this is interim solutions until I find better solutions.
The source code is on GitHub at https://github.com/wtos03/WasteFoodMgmt
And this Video is my result of running the code
https://www.youtube.com/watch?v=--gjYTqpD3c&feature=youtu.be
In this video, you will see when moving knob the LED will move from left to right and right to left depend on the position of the knob. If the LED reaches the Last LED it will
go to the first LED again. However, If you at the first LED you cannot move back anymore only forward is allowed. When you pull up and down the knob. The working LED will on and Servo will move. The knob is toggle state that means pull up/down once to on and again to off.
Next step to program for control DC Motor and create valve using Servo for EM substance control.
Top Comments