LEGO EV3 DNA Scanner

LEGO EV3 Mythical Creatures DNA Scanner

Story

Starting from the will to produce something original for this year science project we put our heads together and came up with this reviewed design of an existing LEGO Mindostorms robot called MR.B3AM. Unlike that design though our LEGO EV3 DNA Scanner does a lot more than “chewing” a monochrome brick to say it’s length, ours scans a sequence of 8 2×2 bricks which can be red, green, blue and yellow, compare that sequence to an internal database and report back if the sequence matches that of a mythical creature. How’s that for a project!

What were we trying to solve

The basic idea is pretty simple. The DNA has four combinations:

  • AT
  • TA
  • CG
  • GC

The colour sensor which is part of the LEGO EV3 Mindstorms set can detect 8 colours amongst which blue, green, yellow and red. We assigned a colour to each combination:

  • AT – Blue
  • TA – Green
  • CG – Yellow
  • GC – Red

As we couldn’t build sequences that were too long we decided to limit ourselves to 8 2×2 bricks which offered plenty of combinations for our creatures.

Our DNA

My son decided they were all mythical creatures and he came up with 10 sequences:

Alicorn
Chupacabra
Dragon
Gargoyle
Minotaur
Nessie (Loch Ness monster)
Pegasus
Phoenix
Unicorn
Yeti

Each colour is associated to a number which comes in really handy when needing to store the information in a variable. The sequences were then:

Alicorn 43255542
Chupacabra 44355232
Dragon 33345422
Gargoyle 43542532
Minotaur 55432224
Nessie (Loch Ness monster) 33225243
Pegasus 35242255
Phoenix 22244535
Unicorn 44352432
Yeti 43255235

Building the robot

Now that we had our samples we had to build the robot and modify it along the way to accept a bigger stick than what MR.B3AM was built for. It has to be said that one could build the stick made of 1×1 and of 1xN baseplates but these peaces are rarer to come about and we didn’t want to wait for Bricklink to ship all the ones we needed let alone spending time searching for them.

The opening is wider and taller

As you can appreciate from the photo below when compared to the original Mr.B3AM design ours is slightly taller and the 4 gears are wider apart.

The side shows some of the adjustments

The rest didn’t change much and in fact the motor and the EV3 brick have been mounted as per the original instructions.

Most of the design remained the same

Unfortunately we did not document the changes required in the build process but it shouldn’t be too difficult for others to figure out.

The code

It’s always hard to keep the right balance between mechanics, electronics and software. On some platforms more than others. With LEGO EV3 regrettably the electronics aspect isn’t available as the platform doesn’t really offer any way to be easily expanded with custom made circuits, a shame in my opinion especially in the age of Arduino, ESP and the Raspberry Pi. In our case certainly the focus had been on the code since the build was a mod of MR.B3AM.

What we didn’t get inspiration from was the code for MR.B3AM as quite naively we initially thought we knew better 🙂 we didn’t 🙁

The program finally on our EV3 brick

The visual platform lends itself to be rather intuitive and that’s were my son spent most of his time.

In the first part his task was to develop the main program which was in charge of moving the DNA stick in front of the colour sensor, retrieve the colour of the 2×2 bricks one after the other, say the colour and display it, store each colour on a numeric array names sample, repeat the process 8 times and eject the stick.

Main program part 1

The second part was a lot trickier, at least it has been for me 🙂 With the sample collected from the first part he was then supposed to use my custom block Find_Sample_DB which returns either the name of the beast or Notfound in case the sequence is unknown. With that he fed the output into a switch block which played the noise of the animal and displayed its name. Error was played and Unknown displayed when nothing was found.

Main program part 2

Behind the scenes a series of non trivial custom block made me regret at times to have embarked into this project. Array for example are a thing which is handled in an unusual way and I had to spend some time understanding how to use them. Once I did the ArrayComparison block made possible to know is the sample we had collected was in fact the same of one of our mythical creatures or not. The difficulty here is to render in blocks what I had learned to do in “normal” written languages.

Array comparison

Where blocks fail in my opinion is that at some point you can easily loose the plot especially if the GUI decides to reroute the various wires OMG!! That’s the case of a more complex custom block were I read the known samples off a “DB” file and into a temporary array.

The DB is just a long text file in which I wrote one after the other the numbers of each beast. In multiples of 8 I can go from one to the other and that’s what I did as the read file block moves the index to the next element after each read.

4
4
3
5
2
4
3
2
3
5

The first eight (0..7) are the Unicorn then a bit of Pegasus (8, 9) and so on.

So you open the file and before you close it, although close it eventually you must, to go say to the 6th number in the file you call the read file block 6 times.

This is what the FileToArray does and so that it doesn’t miss the first sequence I created a special switch that does nothing if the offset specified is 0

The block in fact expects as inputs the name of the DB file, a number N indicating where to start looking for a DNA sequence and our collected sample. It returns an array representing the Nth beast’s DNA in our file.

File To Array

With these two custom blocks I could then create a final third which requires as inputs the name of the file and the collected sample to return a string which is either the name of a beast of Notfound.

The File To Array block will return 0s if the file has been read beyond its end, this means that the quest for finding a mythical beast ended up without finding one. The two red blocks and the adjacent switch handle that case, should it not be the case that we got to the end of the file the switch tells us we are still ok and searching.

The first time we enter this block we start reading the DB file from the beginning, we get the first know sequence, which happens to be the Unicorn, then we compare it with our collected sample.

Find Sample Part 1

If they are a match then we found it! If not we increment by 8 our File_Offset variable and move to the next known sequence by returning back at the beginning of the loop.

Find Sample Part 2

When we find the sample or if we went through the whole DB file without a match we break the Sample_in_DB loop. Upon exiting the loop we calculate numerically what sequence was matched and use a switch to return the right DNA sequence. For example if our variable File_Offset is 24 then we know the 3rd creature was a match and we return a string for Dragon. Returning a sting instead of a numeric value made things easier for my son so although not the best approach it was effective for our needs.

Final thoughts

It’s been a great journey and me and my son learned a lot with this platform which is both quite robust and fun to work with. It will be interesting to see how the EV3 works with MicroPython and under Scratch so to also be able to contrast and compare the advantages and disadvantages of each platform which quite obviously have different age target but can still offer some overlapping scope.

I wish the sensors and the accessories wouldn’t cost that much, we love LEGO but the cost of this platform doesn’t help to make it affordable for the casual hobbyist or for the less wealthy. In our case I don’t think we’ll easily venture to buy a giroscope, the ultrasonic sensor or the battery pack.

The fact that the platform is not open to be easily extended is a bit of a blow, I mean LEGO is a brick on a brick on a brick after all and I get that there is a desire and a need to protect the brand but I’m not a big fan of closed systems, I think they are limited and mostly limiting.

All and all it was great fun though and if you happen to have an EV3 you should certainly spend time exploring its possibilities. The platform has been around for quite some time and maybe a new one could be around the corner soon, the fact that matter is that I see quite a few of these being sold on the likes of eBay so maybe it’s a good time to invest in one.

Code

You can find the code and all the required files on my Github repo

MRB3AM

The instructions to build MR.B3AM are available via the LEGO Education website

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.