< < back home

week 10 - input devices


-individual assignment: measure something, add a sensor to a microcontroller board that you have designed and read it
-group assignment: measure the analog levels and digital signals in an input device



PCB

This week we are learning about different sensors or input devices and how we can retrieve analogue data into our microprocessors and read it. I decided to try a distance sensor (sonar), the Ultrasonic HC-SR04.

I redesigned Neil's board in Eagle:

And generated the traces and outline PNG (also added a little personal touch on the traces)

Then generated the milling strategy in FabModules,milled the board with the Roland SRM-20 and soldered all the components.

programming

Time to program the board. We connect the AVRISP2 programmer and the FTDI cable both to the board and the computer.

And the HC-SR04 distance sensor. I didn't solder it directly to the board. Instead I soldered a connector and connect the sonar using jumpers.

Now we open Arduino and choose these settings:

For the coding I googled a tutorial and copied the code into Arduino (remember to change the pin numbers)





However it will not work because the code is too heavy for ATtiny45 and an error will appear.
We need to change the library. To do that, we will open the Software Serial example and copy these lines...

...into our code (changing the pin numbers) so we can include the SoftwareSerial library:


And now we can compile and upload. Now if we open the monitor we will see what the board is feeling through the sonar. Weird characters appear. That is because the baudios in the code do not correspond to te baudios in the monitor:

If we change that, everything will work and we can now read the board:

Here is the final code I used:


  #include < SoftwareSerial.h >

  SoftwareSerial mySerial(1, 2); // RX, TX

  const int Trigger = 4;   //Pin digital 4 para el Trigger del sensor
  const int Echo = 3;   //Pin digital 3 para el Echo del sensor

  void setup() {

    pinMode(Trigger, OUTPUT); //pin como salida
    pinMode(Echo, INPUT);  //pin como entrada
    digitalWrite(Trigger, LOW);//Inicializamos el pin con 0

     mySerial.begin(9600);
    mySerial.println("Hello, world?");

  }

  void loop()
  {

    long t; //timepo que demora en llegar el eco
    long d; //distancia en centimetros

    digitalWrite(Trigger, HIGH);
    delayMicroseconds(10);          //Enviamos un pulso de 10us
    digitalWrite(Trigger, LOW);

    t = pulseIn(Echo, HIGH); //obtenemos el ancho del pulso
    d = t/59;             //escalamos el tiempo a una distancia en cm
    if (d < 10) {
      mySerial.println("boom");
      mySerial.write('1');
    }
    else {
      mySerial.write('2');
    }
  }


download files


Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.