Digistump Forums

The Digispark => Digispark (Original) Support => Topic started by: aand on December 03, 2016, 12:05:29 pm

Title: Analog Input - Voltage Divider Required?
Post by: aand on December 03, 2016, 12:05:29 pm
I'm using a 4-digit seven segment led and a sunkee soil moisture sensor on a digispark attiny85 usb development board.  I'm using the Arduino ide to program the attiny85, and I have things working, kind of.

I have two questions:

1. Is the max voltage input 3.3v or 5v?  Do I need a voltage divider if input is ~5v or am I okay as-is?

[spoiler]The sensor I'm using is like this:
https://www.amazon.com/SUNKEE-Soil-Hygrometer-Moisture-Sensor/dp/B00AYCNEKW/ref=sr_1_3?s=industrial&ie=UTF8&qid=1480794195&sr=1-3&keywords=soil+moisture+sensor
[/spoiler]

2. I'm reading the soil moisture sensor analog input on P2 analogRead(0), but readings seem flipped.  In dry air it reads ~1016.  In water it reads ~275.  Is this correct?  I may have to adjust my thinking.

This is new to me, so I will not be insulted by any comments.
Thanks!
Title: Re: Analog Input - Voltage Divider Required?
Post by: PeterF on December 03, 2016, 11:05:27 pm
That sensor will be fine to use. The 3.3 - 5v reference is just indicating you can use it with 3.3v or 5.0v stuff and it will still work fine, and as long as you supply it with the same voltage as your microcontroller, it wont do it any harm.

I can't say from experience with that particular sensor, but that does makes sense... sort of... it's saying there is is little connection between the probes when in dry air, and a very good connection when in water. The range of the ADC is 0 - 1023. If you want to reverse the range, you could use the map function in Arduino (https://www.arduino.cc/en/Reference/Map), and just invert the high and low input readings, or simply subtract 1023 from the reading.
Title: Re: Analog Input - Voltage Divider Required?
Post by: aand on December 04, 2016, 09:07:48 pm
Thank you.  And I love the map function -- I can display something that makes intuitive sense to me.  You saved me a lot of time.
Title: Re: Analog Input - Voltage Divider Required?
Post by: Shaker on January 10, 2017, 04:35:56 am
Hi.

Maybe I can "hijack" this topic, because my questions are close to what's being discussed here. I'm very new to microcontrollers at all, but not new to programming.

Let me try to sum up what I understood so far and ask some questions:
The analog input pins accept voltages up to the voltage provided via VIN. Correct?
This voltage is going to be read as the maximum value by analogRead(). That is 255 or 1023?
I read about analogReference() on Arduinos. Do we have to set this, too on the Digispark? This would be: analogReference(DEFAULT);
Is it correct, that I can connect a 12V power source directly on e.g. PIN 0 to read its voltage as long as the reference voltage is equal or higher than this? No resistor? Is the resistor on PIN 0 high enough to restrict the current? Or will any unrestricted power source burn my board?

I want to run the digispark in a car, using the generator as its power supply. Do I have to do something before I connect the generator current (14V) to VIN? Are capacitors necessary or will the regulator on the Spark deal with all the cranks and what's left in the generator current?

Thanks a lot!
Title: Re: Analog Input - Voltage Divider Required?
Post by: PeterF on January 12, 2017, 12:02:25 am
The analog input pins accept voltages up to the voltage provided via VIN. Correct?
Sorry, incorrect. The analog inputs accept voltage up to that provided by VCC - i.e. 5v.

This voltage is going to be read as the maximum value by analogRead(). That is 255 or 1023?
The maximum value should be 1023, which is approximately equal to the VCC voltage - i.e. 5v.

I read about analogReference() on Arduinos. Do we have to set this, too on the Digispark? This would be: analogReference(DEFAULT);
You can probably set it, but there is no need to, as well, you want the default, and specifying that you want the default which is already set to be default is a bit redundant - although it does mean when reading the code that your intentions are pretty clear! ;)

Is it correct, that I can connect a 12V power source directly on e.g. PIN 0 to read its voltage as long as the reference voltage is equal or higher than this? No resistor? Is the resistor on PIN 0 high enough to restrict the current? Or will any unrestricted power source burn my board?
Nope, not unless you want to have a dead spark... with a side-dressing of smoke! The simplest way to measure above the VCC voltage (i.e. 5v) is to use a voltage divider - two resistors, which then allows the digispark to see a scaled down version of the high voltage (12v in this case). However, due to how car voltages can spike (particularly when starting), it may be better to consider using a transistor or opto-isolator (http://playground.arduino.cc/uploads/Learning/Level_shifting_4_arduino.pdf) to buffer the Digispark so it doesn't get zapped.

I want to run the digispark in a car, using the generator as its power supply. Do I have to do something before I connect the generator current (14V) to VIN? Are capacitors necessary or will the regulator on the Spark deal with all the cranks and what's left in the generator current?
I would recommend using a switching power supply to drop the voltage into the digispark... the on-board regulator will probably get pretty hot with anything above 10-11v, so the up to 14.4v-ish of a car is probably going to make it nice and toasty. Doesn't have to be anything fancy... a cheapy like this adjustable switching regulator board will do nicely (http://www.ebay.com.au/itm/MP1584-Adjustable-3A-DC-DC-Power-Converter-Step-Down-Voltage-Regulator-Premium-/201346125234?hash=item2ee12a0db2:g:DeMAAOSw6BtVTIeZ). You would power it off the car, and adjust the screw on the regulator until you get something like 7-8v on the output, and then power the Oak off that via the VIN and GND pins.

Hope that all makes sense... please do ask if you're still stumped on anything! :)
Title: Re: Analog Input - Voltage Divider Required?
Post by: exeng on January 12, 2017, 09:36:10 am
Here's something you might consider to drop the voltage from the battery...
http://digistump.com/products/253
Title: Re: Analog Input - Voltage Divider Required?
Post by: Shaker on January 17, 2017, 02:27:15 am
Thanks a lot, PeterF. Sorry for the delay, I wasn't notified about your excellent reply.

I've been reading and experimenting with my little project in the meantime, so I also found out about the 5V reference and the range of 0-1023. And I have not connected any pin without a resistor or voltage divider ;)

I got the Spark to nicely read the voltage I provide and it's surprisingly precise. I map the analogRead()-value to the range of 0 to 5000 (milli volts) and then multiply with 2 because I have 2 equal resistors and PIN2 in between them. Fine.

I would give it a try and power it directly from the car's output. It would be the nicer solution. Any load will be accomplished via transistors and relays. During my experiments with 12V from a battery it doesn't get so warm.


BUT I have another problem now:
I need to use PIN4 (USB-) as an OUTPUT in the final project.
During development I found DigiKeyboard to be suitable to give me a direct feedback on the analog values read.
Of course, i cannot initialize PIN4 as OUTPUT, as long as I have USB connected, because I want to use Digikeybard. But I made some attempts to do pinMode(4, OUTPUT) and leave USB disconnected after upload.

It worked fine. I finished my experiments at 1pm. Board power off. I continued at 5pm. Now the spark ONLY connects as the bootloader and doesn't even seem to try to connect as the DigiKeyboard. bootloader on - bootloader off - bootloader on - bootloader off... and the loop() doesn't run either. When I disable all Digikeyboard code and start the Spark without USB connected, setup() and loop() run ok.

What's that?

Can you destroy anything if you set PIN4 to OUTPUT and HIGH and connect USB to a Laptop? This might have happend in one of the last minutes at 1pm. And can the Spark be destroyed in a way that it only connects with the bootloader but nothing else?


And thanks, exeng. I've been using these step-down regulators from time to time.
Title: Re: Analog Input - Voltage Divider Required?
Post by: PeterF on January 17, 2017, 04:22:26 pm
No problem.... just glad you found out about the 5v limitations before you had a fried Digispark ;)

I'm not familiar enough with the DigiUSB stuff to really comment on anything there. From a electrical standpoint, I can't see any reason anything would have failed... the 3v6 zener diodes and in-line resistor on P3/P4 are there to prevent any voltages above 3.6v and limit current, I don't think anything should have burnt out (unless maybe the zener got zapped by the digispark being high and then the extra current from the usb???). Hopefully someone else may have some knowledge of this or have had similar problems and know why...
Title: Re: Analog Input - Voltage Divider Required?
Post by: Shaker on January 18, 2017, 05:25:53 am
[...] From a electrical standpoint, I can't see any reason anything would have failed... the 3v6 zener diodes and in-line resistor on P3/P4 are there to prevent any voltages above 3.6v and limit current, I don't think anything should have burnt out (unless maybe the zener got zapped by the digispark being high and then the extra current from the usb???). [...]
What makes me confused is the fact, that the bootloader starts up and the USB communication is correct so that the sketch can be uploaded. This  would exclude the possibility of burnt components, wouldn't it?

Think, I simply buy another one to see, if this one is broken, even when we can't explain why ;)

I'm wondering - if using a voltage divider made up of resistors around 100k or so, will a voltage of like 10V on one of the resistors (not the first one) still cause damage to the board? I mean, the current is restricted by the divider resistors...