TinyCircuits Forum

TinyCircuits Products => TinyLily => Topic started by: dakong27 on October 08, 2013, 09:49:51 AM

Title: Running hc-sr04 ultrasonic sensor with TinyLily
Post by: dakong27 on October 08, 2013, 09:49:51 AM
I'm working on an autonomous helium/hydrogen-filled toy zeppelin for my kids' room that uses a TinyLily with an HC-SR04 ultrasonic proximity sensor to detect when it's approaching a wall and then turn until it can fly some more, flashing LEDs and producing sound effects like "Ahoy there!" all the while.

Weight is a concern, which is why I'm trying to do it with the TinyLily so the envelope of the zeppelin can remain small enough to provide sufficient lift but still maneuver within their room.

I have gotten the sensor to work with an Arduino Uno, but not with the TinyLily.  The voltmeter says the Uno's supplying 5V but the TinyLily's only supplying 3.3, below the 4.5 minimum the sensor needs.  Here are the specs for the sensor:


            Parameter    Min    Typ.    Max    Unit
Operating Voltage   4.50    5.0     5.5     V
Quiescent Current   1.5      2        2.5     mA
Working Current      10       15      20       mA

I am a beginner with electronics, and am stuck at this point.  What is the best way to get the sensor to work with the TinyLily?  Is there a way to bump up the voltage so the sensor will work? 

Thanks in advance for any pointers you guys might have.
Title: Re: Running hc-sr04 ultrasonic sensor with TinyLily
Post by: dakong27 on October 08, 2013, 01:38:41 PM
An update--I saw in the specs for the TinyLily Mini USB Adapter that there is a "resistor jumper option" to toggle output on the TinyLily between 5V and 3.3V, and that the default is 3.3V.  It sounds like the solution to my issue.

However, looking at the TinyLily Mini USB Adapter there doesn't appear to be a jumper like you'd have in an old desktop computer.  I looked at the schematic here: http://tiny-circuits.com/wp-content/uploads/2012/09/ASL2111_Rev2.pdf (http://tiny-circuits.com/wp-content/uploads/2012/09/ASL2111_Rev2.pdf), which indicates a jumper but it's not clear what it looks like on the board or how to switch it.

Does anyone know how to do this?
Title: Re: Running hc-sr04 ultrasonic sensor with TinyLily
Post by: TinyCircuits on October 09, 2013, 09:08:41 PM
Sorry for the delay in getting back to you - yes, the USB Adapter for the TinyLily Mini has a resistor jumper option, the attached picture shows the location of this.  By default the resistor is put in the 3.3V spot, if you desolder it and move it over one position, it will supply 5V instead.

If you're not connected to the USB, you can also run the TinyLily Mini up to 5.5V using an external power supply or battery, just wire this into one of the - and + pins on the TinyLily Mini and this will power it up.

Thanks!

Ken
TinyCircuits
Title: Re: Running hc-sr04 ultrasonic sensor with TinyLily
Post by: dakong27 on October 20, 2013, 12:04:02 AM
Thanks, Ken.  The 5V-jumpered USB adapter arrived this afternoon so I hooked it up to the TinyLily and tried the sensor (on the TinyLily, the sensor's trig pin is connected to the 3 pin, the echo pin to the TinyLily 2 pin.).  Turning on the Serial Monitor, it's still returning gobblety-gook.  I know the sensor, the circuit, and the sketch are fine because swapping out the TinyLily for an Arduino Uno works, returning normal readings like "3 in...8 in...14 in."  I've successfully tried the sensor and circuit with an Arduino Micro as well.   

I have gotten the Blink sketch to work with the TinyLily on a breadboard, so I know the TinyLily is fine.  Do I need to take additional steps to get sensors working with TinyLily? 
Title: Re: Running hc-sr04 ultrasonic sensor with TinyLily
Post by: TinyCircuits on October 20, 2013, 12:50:04 AM
Can you post the code or email it to me at: kburns@tiny-circuits.com?

Are you using the reference pin for the ADC?  If so, the TinyLily doesn't have one, so you'll need to use the VCC rail as the reference voltage.

Thanks,

Ken
Title: Re: Running hc-sr04 ultrasonic sensor with TinyLily
Post by: dakong27 on October 22, 2013, 11:16:29 AM
Hi Ken,

Here's the code:

Code: [Select]
#define trig 3
#define echo 2
#define led 1

void setup() {
   pinMode(trig, OUTPUT);// attach pin 3 to Trig
   pinMode (echo, INPUT);//attach pin 2 to Echo
   pinMode(led, OUTPUT);//attach pin 1 to LED
  // initialize serial communication:
  Serial.begin(9600);
}

void loop()
{
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
 
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
 
  duration = pulseIn(echo, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
 
       
  if(inches < 4){
      digitalWrite(led, HIGH);   // set the LED on
    delay(1000);              // wait for a second             // wait for a second
  } else if (inches>=4){
    digitalWrite(led,LOW);
  }

 
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
 
  delay(100);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}




I don't understand what you mean by "reference pin for ADC" or the rest, unfortunately.  On the Arduino platform I understand "plug line A into port A" and read the output in the code.  As a software guy, it works and that's great but the physical mass of even the smallest Arduino form factor does not work for my project.  I know I'm asking annoying noob questions and am loathe to invite a teachable moment, but if you could explain what you mean I would appreciate it, and perhaps through Google many others in my shoes would appreciate it too.
Title: Re: Running hc-sr04 ultrasonic sensor with TinyLily
Post by: mrbbp on November 21, 2013, 06:01:14 PM
Hello dakong27

ADC: Analog to Digital Converter?

but it seems you connect pin 1 to your led...
you can't use the pin 1 when you use Serial!
cause Serial is on pin 1
use an analog out
Code: [Select]
#define led A0wire the led on A0...
and use it as a digital pin with
Code: [Select]
digitalWrite(led, HIGH)
my 2 cents
Title: Re: Running hc-sr04 ultrasonic sensor with TinyLily
Post by: mrbbp on November 21, 2013, 06:06:47 PM
read this
http://forum.tiny-circuits.com/index.php?topic=127.15

about pin 0 and 1 that seems to doesn't work when usb is plugged... ;)