Arduino <> Firmata <> Visual Basic .NET
- Tutorial
In this article, I want to do a review of a .NET library for interacting with the Arduino hardware platform.
Since in the open spaces of Habr, I did not see a mention of this library, I decided to take this initiative into my own hands.
In this article I will give examples exclusively in the VB.NET language, but you can use this library in any other .NET language.
While developing a specialized complex to perform the tasks I need, I ran into the problem that, under my needs, all the code can do not fit into the memory of the microcontroller, and also taking into account the fact that the device should interact with a PC or, in the absence of a PC, give the same functionality.
Arduino <> Firmata <> Visual Basic .NET
As a result of the searches, I came across the Firmata protocol for the Arduino hardware platform, and since I was gathering dust on the Arduino Mega 2560, I decided to use it. In the process of acquaintance with me, for some reason, this protocol began to be liked less and less until I came across the Andrew Craigie page , which wrote a library for the interaction of the Firmata protocol with .NET languages.
The author’s site provides free examples and source codes, which you can download yourself, in principle, and there are also ready-made modules that you can use for yourself.
Ready-made module for using the digital outputs of the Arduino board.
Ready-made module for working with the analog inputs of the Arduino board.
But I recommend using only the component Firmata.Vb
Examples and how to use
In order to work with this library, it is enough for us to download from our examples into our Arduino: Standart Firmata.
This is where programming with the board is finished for us.
Now, having loaded the studio and adding the component, we can get to work
Hello World >> Led ON!
In order to simply light the LED connected to the board port, it is enough to register the following code:
Add the FirmataVB component to the form
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
FirmataVB1.COMPortName = "COM5" ''Указываем наш COM порт платы
FirmataVB1.Baud = "57600" ''Указываем скорость порта
FirmataVB1.Connect() ''Подключаемся к плате
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
FirmataVB1.PinMode(6, 1) ''Указываем номер вывода и его состояние, в нашем случае 6-й вывод на выход
FirmataVB1.DigitalWrite(6, 1) ''Зажигаем светодиод на 6-м выводе
End Sub
Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
FirmataVB1.Disconnect() ''Закрываем порт при закрытии программы
End Sub
End Class
Pwm
In order to use PWM (PWM), you need to make small changes in the code.
FirmataVB1.PinMode(6,3) '' где 3 - это переход в режим PWM
FirmataVB1.DigitalWrite(6,x) '' где x - это значение от 0 до 255
Analog
To work with the analog input, we specify
FirmataVB1.PinMode(6,0) ''где 0 - перевод в режим входа
x = FirmataVB1.AnalogRead(6) '' где x - переменная в которую мы получаем значения порта, и 6 - порта
Conclusion
As for me, the library was written not in vain and I personally saved a lot of time.
I hope this article will be interesting and useful to someone.
Website of the Firmata.VB.NET library
Website of the Firmata protocol
PS: If it turned out a little informative or there are questions, I’m ready to answer any of them