Learn to use Microcontrollers in a day to enhance Odroid based projects!

Post Reply
L67GS
Posts: 425
Joined: Wed Apr 22, 2020 3:02 pm
languages_spoken: English, Jibberish, Pig Latin
ODROIDs: XU4, C1+,(3) C0's, and a whole big pile of accessories, VU7A Plus,, ect....
Has thanked: 124 times
Been thanked: 59 times
Contact:

Learn to use Microcontrollers in a day to enhance Odroid based projects!

Post by L67GS »

A couple of years ago on a Sunday morning I downloaded an open source software called LDMicro and everything I knew of electronics changed all at once. By dinner time I had my first fairly large program running in LDMicro’s built-in simulator and after that day I honestly have never really thought about doing a project strictly with conventional logic or analog electronics.

Microcontrollers can be a useful augmentation to an Odroid project as well. Maybe the Odroid we’re using doesn’t do PWM for instance, no problem, we’ll shoot some values over UART/I2C/SPI to a microcontroller and that microcontroller (depending on selection) can have multiple PWM outputs ready for our Odroid to command.

How about a shut down timer? Say we need to turn off the power after our Odroid has finished shutting down, no sweat. We can use a microcontroller to make pretty literally any timer we want in a few seconds.

Programming a microcontroller has a couple of different learning curves depending on method, each microcontroller will have it’s native language and instruction set or compilers that can compile C or some other language for it and therein lies the big hurdle for the hobbyist.

LDMicro is a shortcut, it’s fast and easy to learn how to use Ladder Logic and LDMicro provides a GUI Ladder Logic IDE that compiles code for quite a few PIC and Atmega microcontrollers.
The code generated by LDMicro is less than optimal so it does not make perfectly efficient use of program memory, in all but the most extreme cases the microcontroller has so much memory that we’re still only using a fraction of it anyway.

My intention here is just to provide a basic understanding of Ladder Logic, there are plenty of online resources to learn Ladder so I won’t try to re-invent that particular wheel today.

The initial version of LDMicro was written by Johnathan Westhues and is still maintained and upgraded by community in the LDMicro Forum.
The LDMicro page is an interesting read – BUT – to get the current version of the program we need to look through the forum for threads starting with “Ldmicro GitHub news” and follow the link.

Device programmers can be had for under $60.00 USD, and microcontrollers are available at most of the electronics haunts cost effectively (I get mine from Mouser Electronics ).
Any device programmer that supports the device you’re working with will work, but I’d strongly recommend a Minipro TL866, there are several Minipro TL866 versions and updates (and some versions can be hacked into better versions) so it’s worth it to look closely at supported device lists and shop around and such before buying one from a major outlet. It’s unlikely a hobbyist (or even most professionals) would outgrow a Minipro TL866 quickly because the supported device list is truly massive.
Do also (as with anything) familiarize yourself with the microcontroller’s data sheet as some pins may be input/output specific and know what clock frequencies they run at.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

We should all be relatively familiar with the function of a common relay, power the electromagnetic coil and it pulls the switch contacts together:
*
37-Electromagnetic-relay-generic-symbol-1024x446.jpg
37-Electromagnetic-relay-generic-symbol-1024x446.jpg (30.68 KiB) Viewed 359 times
*
Ladder logic is largely based on contacts and coils, contacts can be internal to the program such as in a relay or external like a switch. Contacts that are external are the representation of a physical real world input pin. Coils are the same idea, internal like a relay, or external representing a physical real world output pin.

Looking at the image below we can see a ladder with 2 "Rungs", in the first rung there's a set of contacts whose name is labelled with an X to show it's an input and the rung ends with a coil whose name is labelled with an R to show it as part of a relay so it's internal.
In the second rung we find the set of contacts also labelled R and named the same as our internal coil to show it as the other half of the relay, followed by a coil labelled with a Y to specify it is an output pin.
This is not a practical program, all it will do is pull the output pin high when the user pulls the input pin high, realistically we could eliminate the relay and do it on a single rung if we wanted to waste a microcontroller on this but I wanted to show these relationships.
*
b.jpg
b.jpg (82.04 KiB) Viewed 359 times
*
So now I'll jump in a little further with the next ladder, this counter is still simple but significantly more complex than the first. This ladder will count 10 input pulses and then cycle an output pin high for 1 second, reset itself and do it again.
In the first rung we have our input pin (XInputContact) followed by a OSR (one-shot rising) so that's a switch debouncer that sends one pulse on the opening edge of an input. So if we pull that pin high indefinitely the one-shot will still only allow a single pulse to be seen.
This is valuable in ladder because as the ladder is scanned and run that input will send a pulse with every pass if not for the one-shot. After the one-shot there's an ADD instruction, I have a variable (var0) defined there as well. The ADD instruction adds 1 to var0 each time the input is cycled high.
In the second rung there a COMPARE instruction, this one is "compare for greater than or equal", if or when the criteria is met it will enable and allow the following instructions in the rung to go high. What follows is a TOF timer (turn off delay) with a setting of 1 second, so as it is pulled high it will allow what follows to be pulled high and stay high for 1 second even if it's input is pulled low so that coil (which represents an output pin) will be high for 1 second in the real world.
Now just before the TOF the ladder has a lower connection in the same rung and that goes to a MOVE statement, a move is as it sounds, it moves a value to a variable or between two variables. In this case we're moving "0" to var0 so the counter in the first rung can start counting again from 0.
*
c.jpg
c.jpg (100.77 KiB) Viewed 359 times
*
The ladder below is a stripped down version of the timer I used on my Candice robot project to keep the system power up while the Odroid C0 safely shuts down (the real program I ran is larger because it does a few other things simultaneously).
In this first rung is a thing I like to do in the start of all my programs, I keep that rung there all the way through development and then delete it in the final version. At the start is a TCY (cyclic on/off timer), this one is set to 500ms so it's high 1/2 second and low 1/2 second over and over to simply pulse an output pin that I connect an LED to.
There's things that can go wrong with programming like incorrect configuration bits, or there could be a silly reason the microcontroller isn't running like power, or there could be an error in my program. Having my status LED tells me that the microcontroller is programmed and running, so if it's not acting properly I know it's a mistake in my ladder.
The second rung is the safe shutdown timer, I won't go through it fully but essentially the one-shot fires once when the microcontroller starts, pulling the output coil high, and if the input pin isn't pulled high in 60 seconds the output coil will go low, switching off a BJT or MOSFET that powers the project in the real world. If the input pin is pulled high (like from an Odroid GPIO pin) then the output stays high until 60 seconds after the input goes low.
*
e.jpg
e.jpg (108.73 KiB) Viewed 359 times
*
In this next ladder is a simple PWM I just made, it should work but I haven't bench tested it, lol.
Now we CAN do data to send values (like PWM duty cycle) and words/letters back and forth but this is a simple demonstration of PWM using nothing but an input GPIO to enable it, I won't go through the whole thing in detail but looking at it if the input is pulled high it will increase the PWM duty cycle from 0% up to a max of 100% by incrementing the duty cycle variable once every 100ms until 100% is reached. When the input is pulled low it decreases the duty cycle variable one increment every 100ms until it reaches 0.
In this ladder there's a "COMPARE for less than or equal" and a "SUBtract" instruction too.
*
h.jpg
h.jpg (116.12 KiB) Viewed 359 times
*

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

What I've done here is just really lightly touching on all of this, there are a ton of different instructions that I didn't get into. Before even looking at data transmission there's obviously more compare functions, timers and much more all available in a simple GUI.
I'd encourage anyone who may be interested in programming microcontrollers to download LDMicro and play with it, by all means look at some of the ladder logic tutorial sites too.
There's more stuff to find in the bottom of these pictures too, popup menus will help to assign pins to PWM, contacts and coils. And there's more configuration in the menu on top to set oscillator frequency and other options.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

So setting up a microcontroller workstation after you get the idea of ladder logic is pretty simple and cost effective too, we need a device programmer, an assortment of microcontrollers, some crystals (look at the microcontroller's data sheet to see what oscillator frequencies it can do, I keep 16mhz and 20mhz around), and some 20pf - 30pf caps for oscillator circuits.
** A quick word on oscillators:
Crystal oscillators are not always necessary, almost all microcontrollers have internal R/C oscillators that work fine, if/when we do stuff where precision timing becomes more critical or needs to be faster (mainly sending & receiving data) it's time to use a crystal oscillator which is really just a crystal and 2 caps.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Everyone has their favourite devices, mine are PIC12F683 (8 pin), PIC16F628 (18 pin), and PIC16F886 (28 pin), those 3 devices cover pretty much anything I could want to do but there are many many different PIC and AVR microcontrollers that work with LDMicro.

Thanks for reading, hopefully we'll see some more makers here sprinkling microcontrollers into projects!
These users thanked the author L67GS for the post:
OnosTech (Tue Mar 02, 2021 6:08 am)

mad_ady
Posts: 9250
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, C4, N1, N2, H2, Go, Go Advance
Location: Bucharest, Romania
Has thanked: 599 times
Been thanked: 622 times
Contact:

Re: Learn to use Microcontrollers in a day to enhance Odroid based projects!

Post by mad_ady »

That's a very nice write-up! But I'll admit, it went a bit over my head... I'll need to do more reading to learn how to use microcontrollers
These users thanked the author mad_ady for the post:
L67GS (Sat Jan 30, 2021 3:05 pm)

User avatar
rooted
Posts: 8423
Joined: Fri Dec 19, 2014 9:12 am
languages_spoken: english
Location: Gulf of Mexico, US
Has thanked: 740 times
Been thanked: 330 times
Contact:

Re: Learn to use Microcontrollers in a day to enhance Odroid based projects!

Post by rooted »

Never heard of ladder logic, seems to be a nice way to get a microcontroller doing work

Nice work.
These users thanked the author rooted for the post:
L67GS (Sun Jan 31, 2021 4:34 am)

L67GS
Posts: 425
Joined: Wed Apr 22, 2020 3:02 pm
languages_spoken: English, Jibberish, Pig Latin
ODROIDs: XU4, C1+,(3) C0's, and a whole big pile of accessories, VU7A Plus,, ect....
Has thanked: 124 times
Been thanked: 59 times
Contact:

Re: Learn to use Microcontrollers in a day to enhance Odroid based projects!

Post by L67GS »

mad_ady wrote:
Sat Jan 30, 2021 3:00 pm
That's a very nice write-up! But I'll admit, it went a bit over my head... I'll need to do more reading to learn how to use microcontrollers
That will lead to the microcontroller instruction set, learning simple ladder logic is far easier since microcontrollers will almost all have slightly different instruction sets.
LDMicro takes this out of the equation.

mad_ady
Posts: 9250
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, C4, N1, N2, H2, Go, Go Advance
Location: Bucharest, Romania
Has thanked: 599 times
Been thanked: 622 times
Contact:

Re: Learn to use Microcontrollers in a day to enhance Odroid based projects!

Post by mad_ady »

The problem with PICs imho is the high cost of the programmer, which makes it impractical for small projects.
I wonder if this project would work on usb-programmable boards like attiny or teensy...

L67GS
Posts: 425
Joined: Wed Apr 22, 2020 3:02 pm
languages_spoken: English, Jibberish, Pig Latin
ODROIDs: XU4, C1+,(3) C0's, and a whole big pile of accessories, VU7A Plus,, ect....
Has thanked: 124 times
Been thanked: 59 times
Contact:

Re: Learn to use Microcontrollers in a day to enhance Odroid based projects!

Post by L67GS »

mad_ady wrote:
Sun Jan 31, 2021 3:09 pm
The problem with PICs imho is the high cost of the programmer, which makes it impractical for small projects.
I wonder if this project would work on usb-programmable boards like attiny or teensy...
Not at this time, it's a bunch of Atmega and PIC primarily.

Programmers don't need to be expensive, but cheaper ones are limited where IMO the investment in a Minipro is a solid investment for a home lab. It does PIC, ATmega, EEPROMs, and hundreds of devices I can't remember so it would be a very hard tool to outgrow for the average or even advanced person.

There's plenty of programmers that would do a limited assortment for a few bucks:
https://usa.banggood.com/PIC-Microcontr ... 28d1aa9b43

mad_ady
Posts: 9250
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, C4, N1, N2, H2, Go, Go Advance
Location: Bucharest, Romania
Has thanked: 599 times
Been thanked: 622 times
Contact:

Re: Learn to use Microcontrollers in a day to enhance Odroid based projects!

Post by mad_ady »

Apparently openplc implements ladder logic and can run on linux or on esp8266 boards: https://www.openplcproject.com/runtime/
Maybe it could be made to run on the original Odroid Go as well :)
But yeah, ladder logic would make sense on such embedded controllers.

L67GS
Posts: 425
Joined: Wed Apr 22, 2020 3:02 pm
languages_spoken: English, Jibberish, Pig Latin
ODROIDs: XU4, C1+,(3) C0's, and a whole big pile of accessories, VU7A Plus,, ect....
Has thanked: 124 times
Been thanked: 59 times
Contact:

Re: Learn to use Microcontrollers in a day to enhance Odroid based projects!

Post by L67GS »

That's interesting, LDMicro runs on wine but for whatever reason compiled HEX files end up being larger on Linux than windows. My device programmers are also windows, there's various Linux software but I haven't tried it.

L67GS
Posts: 425
Joined: Wed Apr 22, 2020 3:02 pm
languages_spoken: English, Jibberish, Pig Latin
ODROIDs: XU4, C1+,(3) C0's, and a whole big pile of accessories, VU7A Plus,, ect....
Has thanked: 124 times
Been thanked: 59 times
Contact:

Re: Learn to use Microcontrollers in a day to enhance Odroid based projects!

Post by L67GS »

mad_ady wrote:
Sun Jan 31, 2021 5:41 pm
Apparently openplc implements ladder logic and can run on linux or on esp8266 boards:
LDMicro does esp8266 as well...
Attachments
esp8266.JPG
esp8266.JPG (35.37 KiB) Viewed 271 times

mad_ady
Posts: 9250
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, C4, N1, N2, H2, Go, Go Advance
Location: Bucharest, Romania
Has thanked: 599 times
Been thanked: 622 times
Contact:

Re: Learn to use Microcontrollers in a day to enhance Odroid based projects!

Post by mad_ady »

Nice, then! I wonder if it also supports the wifi part, at least for remote telemetry...

L67GS
Posts: 425
Joined: Wed Apr 22, 2020 3:02 pm
languages_spoken: English, Jibberish, Pig Latin
ODROIDs: XU4, C1+,(3) C0's, and a whole big pile of accessories, VU7A Plus,, ect....
Has thanked: 124 times
Been thanked: 59 times
Contact:

Re: Learn to use Microcontrollers in a day to enhance Odroid based projects!

Post by L67GS »

mad_ady wrote:
Wed Feb 03, 2021 3:15 am
Nice, then! I wonder if it also supports the wifi part, at least for remote telemetry...
I'm uncertain because I've never played with that one, reading on the LdMicro forum is a little vague on that, like it may with the correct header file maybe?

jackinho
Posts: 46
Joined: Mon May 06, 2019 12:09 am
languages_spoken: english, german
ODROIDs: XU4, N2
Has thanked: 14 times
Been thanked: 15 times
Contact:

Re: Learn to use Microcontrollers in a day to enhance Odroid based projects!

Post by jackinho »

Why not use the Arduino IDE? It is well documented and there are tons of example projects and tutorials on the net. I'm not a programmer and I started from zero, but I was able to code the Arduino Nano based in-car power management for my CarPC-project in a short time.

L67GS
Posts: 425
Joined: Wed Apr 22, 2020 3:02 pm
languages_spoken: English, Jibberish, Pig Latin
ODROIDs: XU4, C1+,(3) C0's, and a whole big pile of accessories, VU7A Plus,, ect....
Has thanked: 124 times
Been thanked: 59 times
Contact:

Re: Learn to use Microcontrollers in a day to enhance Odroid based projects!

Post by L67GS »

Arduino is not a microcontroller, it's a little breakout board with a microcontroller on it.
The nano in particular is just an atmega 328 that costs 2X more than just buying an atmega 328, lol.
LdMicro supports many microcontrollers, with a variety of features.
If we're doing a very simple task like a delay turn off a 10f200 would be sufficient, costs $0.60, and fits on the eraser end of a pencil.

OnosTech
Posts: 1
Joined: Tue Mar 02, 2021 6:04 am
languages_spoken: english
Has thanked: 2 times
Been thanked: 1 time
Contact:

Re: Learn to use Microcontrollers in a day to enhance Odroid based projects!

Post by OnosTech »

The best article I have found on the internet for LDmicro so far. I have been using LDmicro for a while now. It is indeed the easiest means of programming microcontrollers. It even supports the programming of the Arduino boards. Funny enough, I found out that there are so little resources for beginners to learn it considering the fact that it brings together the world of PLC and microcontroller. This led me to start a youtube tutorial series on "Microcontroller PLC Ladder Programming with LDmicro".
These users thanked the author OnosTech for the post:
L67GS (Tue Mar 02, 2021 8:03 am)

L67GS
Posts: 425
Joined: Wed Apr 22, 2020 3:02 pm
languages_spoken: English, Jibberish, Pig Latin
ODROIDs: XU4, C1+,(3) C0's, and a whole big pile of accessories, VU7A Plus,, ect....
Has thanked: 124 times
Been thanked: 59 times
Contact:

Re: Learn to use Microcontrollers in a day to enhance Odroid based projects!

Post by L67GS »

Thank You Sir,
Learning ladder logic is learning ladder logic, I read a couple things online and was writing ladder right away.
These users thanked the author L67GS for the post:
OnosTech (Wed Mar 03, 2021 1:54 am)

Post Reply

Return to “General Topics”

Who is online

Users browsing this forum: No registered users and 0 guests