Lesson8. Embedded Programming

by Jiyoung An


Program your board to do something

Boards created so far..

Since I was a behind the schedule throughout the session, I did this assignment along with other electronic assignments, such as week10. Output devices and week13(Input Devices). Therefore the what described in each assignments are not in chronological order. As I have already worked and described in detail on how to work with Servo Motor, Led and Photoresistor, Bluetooth module, I will write more about working with StepperMotor in this documentation, in regard to Final Project. And Summarize basic workflow throughout the whole electronics-assignments to remove redundancies in each post and to gain different aspect. My most detailed documentations on such topic is at week13(Input Devices), which I technically succeeded to solder and program for the first time.

Reading Datasheet even before fabricating your board
Even before actual programming, you should have a clear idea of what your PCB will do. Otherwise, you will end up recognizing that your 'finally-soldered' mill cannot do what you intended to. (I have been there several times). So, It is recommended to study every component that you will be using before actual fabrication of PCB board, and try out with the breadboard with the simple programming. I admit that wiring can be a little bit messy to program your tiny microcontroller on the breadboard as you need to work with larger components and connection with Arduino as a programmer also need to be considered. However, I personally think that it could be the most time-saving and ensuring way especially if you just get into this field for the first time like I did.
Also if you try out in the breadboard already, given the connection is made right, you can even start programming your microcontroller even before your board is made!
You can find datasheet for Attiny chips(24/44/84) from here and datasheet for AtMega328 from here

Tried with adjusting current design by just adding 1x4 pinheader for stepper motor.
Board created with Attiny44
Schematic with Atmega328
Board created
    What I learned by reading datasheets are following:
  • identifiers for the pins- later we can use identifiers of the micro controller in our code.
    Questions that I still have:
  • The used terms are very technical so I am interested in learning more of the meaning to understand more which pin can be used for what (Example PWM)

Embedded Programming
It depends on what you want to do with the the board. So far, I tried out Servo Motor, Led and Photoresistor, Bluetooth module and I will be describing my process with StepperMotor, in relation with Final Project as an example on how I do embedded programming and the code I burn on the microcontroller.
For stepper motor movement and code, I first tried out with breadboard and made variables to change direction, change of speed, and number of rotation. I first used an Arduino since most beginner resources for stepper motors are for the arduino board. I also used a TMC2100 stepper motor driver to run the stepper motor for a microcontroller. The documentation gave me examples which I used to train and understand stepper motor control.

HOW TO: Code to run Stepper Motor
                            
  // stepmotor to control gear
  #define ENABLE 17
  #define DIR 12
  #define STEP 13
  #define CFG1 16
  #define CFG2 15
  #define CFG3 14

 const int stepsPerRevolution = 60;
 unsigned long time;
 int stepCount = 0; //; number of steps the motor has taken

 int Speed = 10; //control how fast to 'step' the motor, 18 for roughly one minute per rotation
 int NumSteps = 200; //angle 1.8degree
 int MicroSteps =16; //microstep where stepperdriver set to - can be 1, 2, 4, 8, 16
 int FullRotation = NumSteps * MicroSteps;

 int Distance = 10; // how far we have carried

 boolean Flag = false; //to set false for initial movement

   // setup function runs once on startup of the microcontroller
   void setup()
   {
      //setting up silentstepstick
     // CFG pins floating
     pinMode(CFG1, INPUT);
     pinMode(CFG2, INPUT);
     pinMode(CFG3, INPUT);

     pinMode(ENABLE, OUTPUT);
     digitalWrite(ENABLE, LOW); //off -active
     pinMode(DIR, OUTPUT);
     digitalWrite(DIR, LOW);
     pinMode(STEP, OUTPUT);
     digitalWrite(STEP, LOW);
   }

                            
                        

Unlike servo motor, stepper motor moves a full rotation. It divides rotation into a number of equal steps.
Input from STEP pin increments the motor and makes the rotation possible. There is also DIR(Direction) pin that you can control. With it, you Step motor needs to be connected with DC and like the previous case, it needs common ground with PCB. Connecting all CFG pins are not necessarily needed, however I initialized and set the input in into them.

                            

   // loop function runs over and over again forever
   void loop()
   {
   digitalWrite(ENABLE, LOW); //active

    digitalWrite(STEP, HIGH);
    delayMicroseconds(Speed);
    digitalWrite(STEP, LOW);
    delayMicroseconds(Speed);
    Distance = Distance +1; //record this step

   //change direction
   if(Distance == FullRotation) {
    // toggle direction
    if(digitalRead(DIR) == LOW){
      digitalWrite(DIR, HIGH);
    }else{
      digitalWrite(DIR, LOW);
    }
   }
 }
                            
                        

This code changes the direction when it reaches a the FullRotation.

                            

   // loop function runs over and over again forever
   void loop() {

  //tikcin
   if(flag == true){
    digitalWrite(ENABLE, LOW); //active

    digitalWrite(STEP, HIGH);
    delayMicroseconds(Speed);
    digitalWrite(STEP, LOW);
    delayMicroseconds(Speed);

    Distance = Distance +1; //record this step
     digitalWrite(LED, HIGH);
   }else{
       digitalWrite(LED, LOW);
   }

  if(Distance == FullRotation) {
     Distance = 0; //reset Distance count
     flag = false; //reset rotation flag
  }

  }
                            
                        

Stepper motor keeps rotating as long as 'flag' is set to true. Whilst LED is turned on.

Using Arduino as an ISP
For most of the time, I have used an Arduino as an ISP to program the Attiny44 microcontroller. I followed the tutorials on Programming an Attiny with Arduino from High-low tech. To use the Arduino as an ISP you first need to load the provided ArduinoISP Code onto the microcontroller of the Arduino board. You can do such by uploading Arduino an 'Arduino as an ISP' program. It is provided as an example project. You just need to do it once.

  • open file from 'File'>'Examples'>'11.ArduinoISP'>'ArduinoISP'.
  • here you just connect Arduino to your computer (no other connection to your PCB board is necessary)
ArduinoISP Configuration

Programming Attiny44
Connect PCB to Arduino - VCC, GNC, SCK, MOSI, MISO, RST needs to be connected to each others
& don't forget about connecting'capacitor' to Arduino board. (short leg with minus sign goes to GND, and the other to RST).

Connecting Board
PCB Configuration
Programming the board

After connecting the attiny to the TMC2100 stepper motor driver and the stepper motor acted strangely (see the video)


for my final project I used the Atmega 328 for which I designed a new board such that the Atmega 328, the TMC2100 stepper motor driver and a bluetooth model fits onto that board. You can read more about that in my documentation of my final project

Soldered Board
Back Side of the PCB
Programming
PCB connected to stepper motor from the back side of the tree
stepper morter runs smoother here



Here you can download file(s) that I created for this assignment: