# Machine control USB using a mega2560

**URL:** <https://discourse.agopengps.com/t/machine-control-usb-using-a-mega2560/1684>\
**Category:** Hardware\
**Created:** [13 April 2020 13:27 UTC](https://discourse.agopengps.com/t/machine-control-usb-using-a-mega2560/1684 "2020-04-13T13:27:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexlan](https://discourse.agopengps.com/letter_avatar_proxy/v4/letter/a/848f3c/32.png) [@alexlan](https://discourse.agopengps.com/u/alexlan)\
**Post date:** [13 April 2020 13:27 UTC](https://discourse.agopengps.com/t/machine-control-usb-using-a-mega2560/1684/1 "2020-04-13T13:27:38Z")

</div>

hi all,  
I have noticed that machine control on a mega does not work. I changed the ports names to the mega controller like PORTD 5 is pin 5 on nano and PORTB 0 is pin22 on mega right?  
It seems when I connect the mega it does not want to communicate to AOG,  
appart changing the ports I did not changed something else.  
I used the latest files v4.1.12

---

<div class="post-metadata">

**Author:** ![alexlan](https://discourse.agopengps.com/letter_avatar_proxy/v4/letter/a/848f3c/32.png) [@alexlan](https://discourse.agopengps.com/u/alexlan)\
**Post date:** [13 April 2020 19:17 UTC](https://discourse.agopengps.com/t/machine-control-usb-using-a-mega2560/1684/2 "2020-04-13T19:17:43Z")

</div>

I looked further in to it. I tried te autosteerusb sketch on mega and connect it to aog, it can connect but the board just freeze only the receive led blinks.  
it is a blank mega without any pin connected.  
its like aog dont like arduino mega boards or visaversa 😛

I would use a mega to have more pins for turning led on and off and use some switches for the uturn signal. just for ease.  
or it can be better to send hydrauliclift byte also through the autosteer port. I can change code to send those byte with i2c to my mega witch is better only one usb to my pc 🙂  
thanks .

---

<div class="post-metadata">

**Author:** ![BrianTee\_Admin](https://discourse.agopengps.com/user_avatar/discourse.agopengps.com/briantee_admin/32/7_2.png) [@BrianTee\_Admin](https://discourse.agopengps.com/u/BrianTee_Admin)\
**Post date:** [14 April 2020 01:13 UTC](https://discourse.agopengps.com/t/machine-control-usb-using-a-mega2560/1684/3 "2020-04-14T01:13:52Z")

</div>

IT should work, aren’t the lower Digital pins the same on nano and mega?

---

<div class="post-metadata">

**Author:** ![alexlan](https://discourse.agopengps.com/letter_avatar_proxy/v4/letter/a/848f3c/32.png) [@alexlan](https://discourse.agopengps.com/u/alexlan)\
**Post date:** [14 April 2020 10:46 UTC](https://discourse.agopengps.com/t/machine-control-usb-using-a-mega2560/1684/4 "2020-04-14T10:46:20Z")

</div>

No they are not but its not even the ports it is just communicating with aog that freeze it. I removed al te output code like bitset and digitalwrite, only communication code stays in and it does the same, it freezes.  
Maybe the mega can not take the uart of aog fast enough. I don’t know.

---

<div class="post-metadata">

**Author:** ![BrianTee\_Admin](https://discourse.agopengps.com/user_avatar/discourse.agopengps.com/briantee_admin/32/7_2.png) [@BrianTee\_Admin](https://discourse.agopengps.com/u/BrianTee_Admin)\
**Post date:** [14 April 2020 13:22 UTC](https://discourse.agopengps.com/t/machine-control-usb-using-a-mega2560/1684/5 "2020-04-14T13:22:48Z")

</div>

IT really really should though. I have a MEGA, i can try and see what happens.

---

<div class="post-metadata">

**Author:** ![alexlan](https://discourse.agopengps.com/letter_avatar_proxy/v4/letter/a/848f3c/32.png) [@alexlan](https://discourse.agopengps.com/u/alexlan)\
**Post date:** [14 April 2020 15:48 UTC](https://discourse.agopengps.com/t/machine-control-usb-using-a-mega2560/1684/6 "2020-04-14T15:48:12Z")

</div>

Yes it should work and I see not why it doesn’t. Or maybe a eeprom thing?  
But anyway if I have the hydraulic lift and u turn via autosteer Im also happy 😛  
Actually could you make a video to show how to change some code in AOG, in the old version I could change some, but now with the .exe application I dont know how to debug and start the program. it gives me… when clikking the start button  
//  
Erreur Impossible de traiter le fichier Forms\FormGPS.resx car il se trouve dans la zone Internet ou Restreinte ou il contient Mark of the Web. Pour traiter ces fichiers, supprimez Mark of the Web. AgOpenGPS 1   
//   
thanks for your time.

---

<div class="post-metadata">

**Author:** ![hagre](https://discourse.agopengps.com/letter_avatar_proxy/v4/letter/h/9dc877/32.png) [@hagre](https://discourse.agopengps.com/u/hagre)\
**Post date:** [15 April 2020 21:52 UTC](https://discourse.agopengps.com/t/machine-control-usb-using-a-mega2560/1684/7 "2020-04-15T21:52:47Z")

</div>

Hello,  
I just found the problem frezzing the MEGA2560:  
if you change the line 84 to →   
unsigned **long** time = currentTime;  
the Mega2560 is not freezing any more (you still need to adapt the correct ports/pins (PORTB,…) or use digitalWrite() to make it more easy

---

<div class="post-metadata">

**Author:** ![alexlan](https://discourse.agopengps.com/letter_avatar_proxy/v4/letter/a/848f3c/32.png) [@alexlan](https://discourse.agopengps.com/u/alexlan)\
**Post date:** [17 April 2020 13:17 UTC](https://discourse.agopengps.com/t/machine-control-usb-using-a-mega2560/1684/8 "2020-04-17T13:17:53Z")

</div>

Thanks for your time but it is still the same and its no port problem or digitalwrite because I just delete that part.  
In the Serialmonitor it shows what it should, but when connecting to AOG it still freezes.

---

<div class="post-metadata">

**Author:** ![BrianTee\_Admin](https://discourse.agopengps.com/user_avatar/discourse.agopengps.com/briantee_admin/32/7_2.png) [@BrianTee\_Admin](https://discourse.agopengps.com/u/BrianTee_Admin)\
**Post date:** [18 April 2020 04:36 UTC](https://discourse.agopengps.com/t/machine-control-usb-using-a-mega2560/1684/9 "2020-04-18T04:36:55Z")

</div>

Can you post the code?

---

<div class="post-metadata">

**Author:** ![alexlan](https://discourse.agopengps.com/letter_avatar_proxy/v4/letter/a/848f3c/32.png) [@alexlan](https://discourse.agopengps.com/u/alexlan)\
**Post date:** [18 April 2020 06:54 UTC](https://discourse.agopengps.com/t/machine-control-usb-using-a-mega2560/1684/10 "2020-04-18T06:54:18Z")

</div>

[Machine\_USB\_forMega.ino](https://discourse.agopengps.com/uploads/short-url/A7LXLeXEWotCEfoAaWGH6JNhtaJ.ino) (7,3 Ko)  
I just change unsigned long and the digitalWrite().

---

<div class="post-metadata">

**Author:** ![hagre](https://discourse.agopengps.com/letter_avatar_proxy/v4/letter/h/9dc877/32.png) [@hagre](https://discourse.agopengps.com/u/hagre)\
**Post date:** [18 April 2020 20:31 UTC](https://discourse.agopengps.com/t/machine-control-usb-using-a-mega2560/1684/11 "2020-04-18T20:31:50Z")

</div>

I have reduced the arduino code to a minimum, but it is still working different with NANO/UNO or MEGA2560.

Compiled with Arduino IDE 1.8.10 on Win10.

[Machine\_USB\_forMega\_minimal.ino](https://discourse.agopengps.com/uploads/short-url/ouRDF8s830Cpe2dEZNFJybJQmFs.ino) (693 Bytes)

The indication in AgOpernGPS **Beta4.2.01** is for the  
**UNO/NANO:** 127,248,0,0,0,0,0,0,0,0  
**MEGA2560:** \*\* Machine Module Not Connected  
the Serial Led is Flashing on the boards, and the Output is exactly the same (checked with Hterm 0.8.1)

![Htem-screenshot](https://discourse.agopengps.com/images/transparent.png)

If you try to repoduce this, use the MEGA first. After the UNO was found, the text in the box stays the same until a complete restart of the program.

The only difference i have discovert is the WindowsDriver  
USB\VID\_2341&PID\_004 **2**.… for MEGA  
USB\VID\_2341&PID\_004 **3**.… for UNO

I found an additional bug, maybe you can help me?  
If i disconnect the arduino bord from the USB port during it is connected in the software, AgOpenGPS freezes forever, even after reset/reboot/… .I have to copy the complete program to a new folder (with a new name) to recover the software. I tryed to delete AG folder in documents, deleted all found regedit points. ???

---

<div class="post-metadata">

**Author:** ![alexlan](https://discourse.agopengps.com/letter_avatar_proxy/v4/letter/a/848f3c/32.png) [@alexlan](https://discourse.agopengps.com/u/alexlan)\
**Post date:** [18 April 2020 21:14 UTC](https://discourse.agopengps.com/t/machine-control-usb-using-a-mega2560/1684/12 "2020-04-18T21:14:31Z")

</div>

I tried it to and AOG froze also for ever ☹, it was when I connected the mega first and then run AOG V4112, it connected automatically, I couldn’t even see the program.  
The code you send works just fine, so its somewhere in between 😛 .

---

<div class="post-metadata">

**Author:** ![hagre](https://discourse.agopengps.com/letter_avatar_proxy/v4/letter/h/9dc877/32.png) [@hagre](https://discourse.agopengps.com/u/hagre)\
**Post date:** [18 April 2020 21:58 UTC](https://discourse.agopengps.com/t/machine-control-usb-using-a-mega2560/1684/13 "2020-04-18T21:58:34Z")

</div>

hello again!

Now i think i found the problem/bug in AgOpenGPS software!

SerialComm.Designer.cs  
Line 519 have to be disabled to work witch my MEGA2560  
([//spMachine.DtrEnable](https://spMachine.DtrEnable) = true;)  
I think the implementation (if there is any) of the DTR pin on the MEGA is not done.

But now i managed to freeze it again (even in the debugging run of VS2019).  
If you had an usb device connected in the last session, the next time during boot it will be reconnected. This is the point in time to freeze, if the old comport or connection is not safely disconned befor shutdown.

Tommorow i will continue to find out more about this problem. Maybe we get some help by @BrianTee_Admin🙏

---

<div class="post-metadata">

**Author:** ![Larsvest](https://discourse.agopengps.com/letter_avatar_proxy/v4/letter/l/50afbb/32.png) [@Larsvest](https://discourse.agopengps.com/u/Larsvest)\
**Post date:** [18 April 2020 22:38 UTC](https://discourse.agopengps.com/t/machine-control-usb-using-a-mega2560/1684/14 "2020-04-18T22:38:17Z")

</div>

Sounds like the total freeze there was many versions ago when trying to connect gps device to wrong USB port. Remember that the solution was to delete one file where the last settings were hidden. Must be on computer to find it again somewhere on combine forum.

---

<div class="post-metadata">

**Author:** ![hagre](https://discourse.agopengps.com/letter_avatar_proxy/v4/letter/h/9dc877/32.png) [@hagre](https://discourse.agopengps.com/u/hagre)\
**Post date:** [20 April 2020 09:52 UTC](https://discourse.agopengps.com/t/machine-control-usb-using-a-mega2560/1684/15 "2020-04-20T09:52:19Z")

</div>

Thank you @Larsvest for the hint with the forum and the settingsfile. I have now found the file(s) under C:\Users\<USERNAME\>\AppData\Local\AgOpenGPS. (Windows10)  
After removing the files it is up and running again!  
Saves me a lot of time. 😌

---

<div class="post-metadata">

**Author:** ![hagre](https://discourse.agopengps.com/letter_avatar_proxy/v4/letter/h/9dc877/32.png) [@hagre](https://discourse.agopengps.com/u/hagre)\
**Post date:** [20 April 2020 11:58 UTC](https://discourse.agopengps.com/t/machine-control-usb-using-a-mega2560/1684/16 "2020-04-20T11:58:57Z")

</div>

What i found out so far:

The communication and indication of the received serial data in AgOpernGPS depends on the implementation of the serial to usb chip (typ of com-chip and schematics on the different arduino boards and clones).  
I have used  
**UNO/NANO:** with CH340G everything is fine -\>127,248,0,0,0,0,0,0,0,0  
**ESP8266 - MX DevKit:** with CH340 everything is fine -\>127,248,0,0,0,0,0,0,0,0  
**ESP32 - NodeMCU - ESP32S:** with C2102 everything is fine -\>127,248,0,0,0,0,0,0,0,0  
**Due on programming port:** (NO EEPROM!) with 16u2 everything is fine -\>127,248,0,0,0,0,0,0,0,0  
**Due on native usb port:** avoid!!! complete freeze of all  
**MEGA2560:** (clone) with 32u2 resulting in problems and freezes -\>\*\* Machine Module Not Connected  
Solving posibilities:

- find and use working orginal/clone with a differnet serial chip (other then 32u2), or maybe update firmware on chip
- cut the connection line at the “RESET-EN” position  
OR  
disable Line 519 in SerialComm.Designer.cs [//spMachine.DtrEnable](https://spMachine.DtrEnable) = true;  
both resulting in NOT reseting the board when connection is established

I think it have to do somthing with the timing of the reset via the USB - DTR pin. Normaly it is working perfect with arduino IDE,… during programming, but not completly wich the serial port implementation in VS2019/AgOpenGPS

After receiving different MEGA2560 board, i will continue to dig deeper in it.

---

<div class="post-metadata">

**Author:** ![BrianTee\_Admin](https://discourse.agopengps.com/user_avatar/discourse.agopengps.com/briantee_admin/32/7_2.png) [@BrianTee\_Admin](https://discourse.agopengps.com/u/BrianTee_Admin)\
**Post date:** [20 April 2020 14:34 UTC](https://discourse.agopengps.com/t/machine-control-usb-using-a-mega2560/1684/17 "2020-04-20T14:34:58Z")

</div>

That is very odd that mega would do that! I will have to see if i have one and test quick like you did - justy load and see what happens

---

<div class="post-metadata">

**Author:** ![hagre](https://discourse.agopengps.com/letter_avatar_proxy/v4/letter/h/9dc877/32.png) [@hagre](https://discourse.agopengps.com/u/hagre)\
**Post date:** [20 April 2020 17:11 UTC](https://discourse.agopengps.com/t/machine-control-usb-using-a-mega2560/1684/18 "2020-04-20T17:11:41Z")

</div>

**Update:** 😁  
I have now tried some versions of **MEGA2560** , but the DTR reset-circuit made troubles.  
I discovert by surprise that connecting an external serial to usb converter on the TX and RX Pins and GND is solving the Problem due to one of the resistors pulling down to GND. I have not invastigated the exact reason. Finaly i found a special version i had in stock with a **CH340G** chip. It is very smal (about 2xnano) with all pins avaliable. **All i tested now works out perfectly.**

![mini mega_smal](https://discourse.agopengps.com/images/transparent.png)

Ebay Link: [mini Mega](https://www.ebay.at/itm/Mini-MEGA-2560-Pro-Micro-USB-CH340G-ATMEGA2560-16AU-For-Mega-2560-R3-Arduino-NEU/233082740772?hash=item3644d06024:g:pAUAAOSw~HFcMa~M)

---

<div class="post-metadata">

**Author:** ![BrianTee\_Admin](https://discourse.agopengps.com/user_avatar/discourse.agopengps.com/briantee_admin/32/7_2.png) [@BrianTee\_Admin](https://discourse.agopengps.com/u/BrianTee_Admin)\
**Post date:** [21 April 2020 01:49 UTC](https://discourse.agopengps.com/t/machine-control-usb-using-a-mega2560/1684/19 "2020-04-21T01:49:41Z")

</div>

That’s a really cool form factor!

---

<div class="post-metadata">

**Author:** ![hagre](https://discourse.agopengps.com/letter_avatar_proxy/v4/letter/h/9dc877/32.png) [@hagre](https://discourse.agopengps.com/u/hagre)\
**Post date:** [27 April 2020 10:09 UTC](https://discourse.agopengps.com/t/machine-control-usb-using-a-mega2560/1684/20 "2020-04-27T10:09:56Z")

</div>

@alexlan  
Update: check this posting - all my MEGA2560 board are now working

> [@Section control code help](https://discourse.agopengps.com/t/section-control-code-help/1817/12):
>
> @BrianTee_Admin It looks like the timing is adjusted, but the purple line is still in the same position/distance of the vehicle. Now I updated the [Machine\_USB\_4202\_16pins\_V3.2.ino](https://discourse.agopengps.com/uploads/short-url/tknObXCjBFeKflKSHQNy56hbz6H.ino) (12,0 KB) it is also avaliable in a pullrequest on Github [https://github.com/farmerbriantee/AgOpenGPS/pull/161in](https://github.com/farmerbriantee/AgOpenGPS/pull/161in) the v4201 branch. (incl. serial connection with MEGA2560 correction in AGOpenGPS) This new Arduino Mashine USB file with the posibility to select up to 16 Relays. (if compiled with MEGA2560) It is possi…

[Next page](https://discourse.agopengps.com/t/machine-control-usb-using-a-mega2560/1684.md?page=2)
