Introduction: TinyANN, Artificial Neural Network Meets ATTINY

Ladies and Gentlemen,

Welcome to my first instructable. It is a long time I follow this excellent site. I learn a lot from this community and I wish to return back the favor by publishing my own contribution.

I do apologize for the English as it is not my native language. Remarks are highly appreciated.

Since I discovered Intructables.com, I began to play with Arduino and robotic. From my educational background, I have some knowledge about Artificial Intelligence. So I thought it would be nice to put them together. Little by little, the idea to build a robot, based on some ATTINY processor and programmed using an Artificial Neural Network (ANN), began to blossom in my mind.

I settled for an “Avoiding Collision Turtle” robot powered by a Fixed ANN. Later on, I will add a self-learning module using a Raspberry-Pi to train the ANN implemented here (Perhaps a next instructable?). Have a look at the video to watch this robot in action.

So let's start!

Step 1: What Do You Need?

Software

  • OpenSCAD 2015.03
  • Fritzing 0.9.2
  • Arduino IDE 1.6.8
  • Inkskape 0.91 (Optional)

Material for the robot

  • (x2) Acrylic sheets
  • (x1) Roller
  • (x2) N20 motor and support
  • (x2) wheel for N20 motor (wheel, motor and support are usually sell together)
  • (x3) L shaped support in metal or plastic
  • (x4) M3 spacer 18mm
  • (x4) M3 spacer 10mm
  • (x2) M3 spacer 4mm
  • M3 screws and nuts
  • (x1) BATTERY LIPO 7.2V

Electronics

Motherboard (x1):

    • (x1) ATTINY84A
    • (x1) H-Bridge L293D
    • (x1) Regulator 7805
    • (x1) Capacitor 100uF (16V)
    • (x1) Capacitor 1uf (16V)
    • (x1) Capacitor 100nf
    • (x1) Resistor 10KR
    • (x1) Resistor 220R
    • (x1) LED (I like blue)
    • (x1) Micro switch (ON/OFF)
    • (x1) Micro Push Switch (RESET)
    • Wires
    • Several connectors

    Sensor (x3):

    • (x1) Resistor 100KR
    • (x1) Resistor 220R
    • (x1) RPR220 IR Emitter/Receiver (or equivalent)
    • (x1) Servo type connector

    Tools

    • Hot glue gun
    • Soldering Iron
    • Screw drivers
    • X-facto
    • Pliers
    • Files

    Disclaimer and Safety

    Mind your safety and others. Some tools or products used in the construction of this robot may hurt. So please be vigilant.

    All materials in this instructable were created by myself (except specified otherwise). However I didn't claim any ownership, feel free to use what ever may be useful to you.

    Step 2: Electronic: Building the Motherboard

    The motherboard features an ATTINY84A chip. I choose this ATTINY because it has 8Kb of memory and enough digital pins for the sensors and the motor controller. This motherboard has a reset button and a programmer connector (J1) using the SPI protocol (SLT, MOSI, MISO, CLK). 3 pins (J2, J3, J4) are used as analog input for sensors, 2 pins (J5, J6) as digital inputs/outputs and the other pins are connected to a L293D to command two N20 motors. I invite you to search on Instructables.com about ATTINY. There are many indestructible about how to use and to program ATTINY and arduino.

    You attached the Fritzing files containing the two layers of this PCB. The top layer contains only few connections with via. They can be replaced by wires for who prefer make a “one layer” PCB. I used the toner transfer method (again Instructables.com is your friend). To align the 2 layers, I drilled 2 via on each side of the PCB before the transfer. With the help of these 2 pins I sandwiched the PCB with glossy papers (one for the top layer (mirror), one for the bottom layer) and feed to a laminating machine. After etching, the result was very acceptable.

    IMPORTANT: Triple check all the copper tracks; with a multi-meter, checks for short circuits or discontinued tracks.

    After soldering the via, I protected the top layer copper using a transparent protective spray. After soldering the components, I used the same spray to protect the bottom layer copper. I also used hot glue at the base of the wires to reinforce their mechanical strengths.

    Use the third picture as reference to place the components. I put connectors for the motors (M1, M2), the sensors (J2, J3, J4), and the programmer (J1). They are 2 free connectors (J5, J6) that can be used for a daughter board or for debugging. I used connectors I found in my workshop. Feel free to use whatever you have or solder the wires directly. The connector J2 to J6 use the same pin arrangements; the pin close to the ATTINY is the signal (SIG), the pin in the middle is 5V and the pin outside is the ground (GND). I soldered wires with a JST connector for the battery. Depending of your battery you may have to choose another connector.

    I created some sketches to debug the motherboard and ensure the sensors and the motors are working. You will find more details how to use them in the later steps. I advise you to first try these sketches before to upload the ANN sketch. If the motors run in the wrong direction, just swap their wires (or change the digital pins in the sketches).

    Step 3: Electronic: Building the Sensors

    You must build 3 of this sensor. The robot uses Infrared sensor to detect obstacles. Basically closer is the obstacles, lower is the voltage at the pins of the IR receiver. The voltage is at its highest when there is no obstacle. These sensors will be connected to 3 analog pins (A0, A1, A2) of the ATTINY.

    This sensor is a single layer PCB and uses the same building process as the motherboard.

    Step 4: Construction: Building the Lower Plate

    I did a prototype using OpenSCAD to check dimensions and positions of the elements. I share those files for your perusal.

    The robot is built of 2 plates cut from acrylic sheets. I had these round plates in my workshop from a turtle robot kit. But you can use the blueprint (png and svg) to get the exact dimension and cut by yourself; use a laser cutter if you have access to it. Or you can buy some turtle robot kit for a very low price. You will need also to build 3 supports (L shape) for the sensors drilling some plastic or aluminium. I also found those in my junk box.

    After gathering those materials, screw the motors and the caster roller on the back of the lower plate.

    Step 5: Construction: Fixing the Battery

    Zip tie the battery on the top of the lower plate and add the spacers. The spacer must be higher than the battery as the upper plate will be fixed to them. The battery should be in sandwich between the plates.

    Step 6: Construction: Building the Upper Plate

    Screw the sensors and the motherboard on the top of the other plate. The sensors are attached to the plate using the L shaped support (See the blueprint or OpenSCAD files).

    Step 7: Construction: Assembling the Robot

    Finally screw the two plates together. Pass the different wires through the hole in the middle of the plates below the motherboard. Attach the connectors. Use as reference the electronic steps.

    And admire your work :)

    Step 8: Programming: Install the Libraries

    The robot is programmed using Arduino IDE. Before to go to the programming, you need to be sure to install some board managers and libraries into Arduino IDE.

    Install the board manager for ATTINY core. Grab the ATTinyCore-1.0.6.1.zip, unzip it in your hardware folder and restart Arduino IDE. If you have trouble, search on Instructables.com for tutorials (It is how I did).

    Install the libraries required to compile the sketches. The libraries are EEPROMEX and YAPID. Grab the ZIP packages from here and here. This libraries were written by myself, inspired by various examples see here and there. Especially I do not claim any ownership on EEPROMEX; the code were found on Arduino.cc. You can use Arduino library manager to add them.

    Before to continue for the next steps, please grab all sketches here.

    Step 9: Programming: Connect the Programmer

    The ATTINY needs a programmer. I used a cheap USBasp from China connected to a computer by a USB port and using a 10 pins connector. Usually the programmer is plug-and-play and it is recognized by the Arduino IDE. The programmer port should appear in the option Port of the Tools menu. If not, please refer to the documentation of your programmer.

    Before to upload sketches, please burn the bootloader by selecting the programmer you have (USBasp for me) with the command in the Tools menu and then by selecting "Burn the bootloader". When the bootloader is successfully burnt, you are ready to program your robot by compiling/uploading a sketch.

    In the picture, you can see I use the free pins (J5, J6) to connect a serial USB interface (TX, RX, GND). By using the SoftwareSerial library, I can use the monitor and the plotter for debugging. This is optional but I strongly advise to setup this interface if you want check the different components works or if you want play with the code.

    IMPORTANT: Do not power on the robot when uploading a sketch. If you power on, the motor may start randomly and you may damage the power regulator.

    Step 10: Programming: Upload the Test Sketches

    Before to upload the ANN sketch, please try the test sketches:

    • Test1: using the serial interface, you can visualize, using the plotter of Arduino IDE, the effect of the sensor when you put on obstacle (your hands) in front of the front sensor.
    • Test2: using the serial interface, you can visualize, using the plotter of Arduino IDE, the effect of the sensors when you put on obstacle (your hands) in front of the side sensors. This one visualize the difference between right and left sensors. If your hand is closer to the left or right sensor, the curve will move accordingly.
    • Test3: it will turn the motor forward, backward, turn right and turn left. If the robot moves in other direction try to swap the wires or the connectors.

    After connected the serial interface and the programmer, open a sketch and upload it. Then turn on the robot. You should see numbers on monitor or a curve on the plotter. Please be sure to select the same speed as the sketches (default is 9600 bauds).

    If all the debug sketches upload and work properly, you won’t have any trouble for the following step.

    Step 11: Calibration

    The robot needs a calibration before to be used.

    Open the “ANN.ino” sketch. In the “config.h”, uncomment the “#define CALLIBRATE“ statement. Power off the robot before to upload the sketch.

    //#define DEBUG
    //#define DEBUG_PRESERVE_EEPROM
    //#define DEBUG_FRONT_SENSOR
    //#define DEBUG_SIDE_SENSOR
    //#define DEBUG_RANDOM_SENSOR
    #define CALLIBRATE

    Be sure to have no obstacles around the robot when you power on. When the first calibration is over, the robot will move forward a bit. Then it will calibrate the front sensor. The robot will move forward until it meet an obstacle. Here it will move forward and backward judging the distance. After it will calibrate the side sensors. It will turn right and adjust the balance between the sensor by turning left and right.

    Please watch the video and the pictures to perform the calibration. As you can see in the video and pictures, I used a rig of cardboard to build an enclosure to perform the calibration.

    Step 12: Here You Go!

    When the calibration is over, power off the robot. Comment the “#define CALLIBRATE“ in the “config.h” and upload the sketch again. Now the robot should move forward and avoid obstacles by its own.

    //#define DEBUG
    //#define DEBUG_PRESERVE_EEPROM
    //#define DEBUG_FRONT_SENSOR
    //#define DEBUG_SIDE_SENSOR
    //#define DEBUG_RANDOM_SENSOR
    //#define CALLIBRATE

    If you want more details about the theory and the code, please have a look at the wiki of the project.

    Thank you