Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

attiny85 for analog input digital output - please help.

Status
Not open for further replies.

coreymanshack

New Member
First of all I would like to note that I am very new to building circuits, the raspberry pi, and attiny85. I have been doing a lot of research on what I'm trying to do but I have a few questions. I want to use the attiny85 to measure voltage of a 1.5V battery as in this schematic and send it to the GPIO on the raspberry pi and read the digital result via python and log it; https://123d.circuits.io/circuits/111623 ... ry-pi-gpio

I will be doing the following with the circuit -
Analog in on pin 2 of the attiny (positive battery terminal, negative side of battery grounded to GPIO) - digital out on pin 7 of the attiny. Then I want to send digital out from attiny to GPIO on pin 3 to read with a python program on raspbian and log it.

I know I need avrdude to program the attiny and a simple DAPA circuit ( I will be using the serial port and passing avrdude the serial option ). What I don't know - is what code to write for the attiny to accept analog input on pin 2 and send the digital equiv out on pin 7. I then don't know how to access GPIO pin 3 with python and read the result to log it. Can anyone help me and/or point me to some documentation on how to do these two things?



Update:
I found this link for interfacing GPIO with python https://sourceforge.net/p/raspberry-gpio ... wiki/Home/ and on the digital section it says the following -

Input
To read the value of a GPIO pin:
GPIO.input(channel)
(where channel is the channel number based on the numbering system you have specified (BOARD or BCM)). This will return either 0 / GPIO.LOW / False or 1 / GPIO.HIGH / True.


So what I don't understand now is how do I measure voltage programmatically from 0 / GPIO.LOW / False or 1 / GPIO.HIGH / True
 
First You have to write a programm with an Compiler AVR GCC, BASCOM an so on.
When that Programm is compiled You get an .hex File that can be taken into the Attiny.

To activate the A/D Converter You have to set some Registers with the wished parameters.
The Attiny has only one A/D Converters with an additional input selector.
So You have to set the input Selector to ADC3 and then make a Conversation of the A/D Converter.
No You have to calculate the current Voltage from the A/D Result and translate them into ASCII signs.
When I'm right the attiny 85 has no Hardware USART's, so You have to use an Software USART.
That's a little programm that generates the serial RS232 Signals.
Some bigger Controllers has an internal Hardware USART that make the generation of serial signals quite easy.

When using a USART I would suggest You to use an external Clock Source ( e. g. Crystal ) to avoid trouble.

The used Registers and there setting You can take from the Datasheet of the Controller.
The calculation of the Voltage depends of the Used Reference an is descibed in the Datasheet.
An Software USART You have to include from an Source in the Internet.
I think BASCOM has one integrated, but I'm not a BASCOM Guy.
 
Last edited:
First You have to write a programm with an Compiler AVR GCC, BASCOM an so on.
When that Programm is compiled You get an .hex File that can be taken into the Attiny.

To activate the A/D Converter You have to set some Registers with the wished parameters.
The Attiny has only one A/D Converters with an additional input selector.
So You have to set the input Selector to ADC3 and then make a Conversation of the A/D Converter.
No You have to calculate the current Voltage from the A/D Result and translate them into ASCII signs.
When I'm right the attiny 85 has no Hardware USART's, so You have to use an Software USART.
That's a little programm that generates the serial RS232 Signals.
Some bigger Controllers has an internal Hardware USART that make the generation of serial signals quite easy.

When using a USART I would suggest You to use an external Clock Source ( e. g. Crystal ) to avoid trouble.

The used Registers and there setting You can take from the Datasheet of the Controller.
The calculation of the Voltage depends of the Used Reference an is descibed in the Datasheet.
An Software USART You have to include from an Source in the Internet.
I think BASCOM has one integrated, but I'm not a BASCOM Guy.

Thank you - I've finally got around to programming my ATTINY85 and I did it with my raspberry pi from the GPIO pins with a simple circuit I found here - https://www.instructables.com/id/Programming-the-ATtiny85-from-Raspberry-Pi/?ALLSTEPS
I uploaded the blink.c program to the attiny85 using the code in this tutorial and made all 5 pins on the pi go from high to low.

I have a few questions though and I'm not sure if I should open a new thread or what? Here is my circuit -




I'm powering this from a USB port on my computer. ( 5v 1A ?)

I measured the input voltage to my voltage regulator at 5v (**broken link removed**)
Why is the output voltage only about 3.6V ?
I'm only reading about 1.3V across each LED (**broken link removed**) does this mean they are drawing 384ma each? Why is the voltage 1.3V and not 5V?
I hooked the LED's to the voltage regulator as the + source and waited for the microcontroller ouput to go LOW for them to turn on - High is off - is this the preferred way to do this?
I know when I had all of my LEDs connected with no voltage regulator the USB port alone would not drive them - I had to install the voltage regulator before they would all blink (attiny85 brown out without voltage regulator?)

Edit: Update, I figured out I don't need the 5v regulator because it is being fed by USB power, and the 5v regulator I'm using requires voltage higher than 5v to work. Need some resistors in series with the LED's because they are being fed by 5v and it could fix my problem.

Edit @#2: - here is my updated circuit.
So I put two resistors in series because I didn't have a 300 Ohm resistor. I had some 100 Ohm 1% Resistors and 220 Ohm 1% resistors. I measured with my multimeter and the 100Ohm resistors were about 99Ohms and the 220 were about 218Ohms. I only had two 220Ohms resistors so my project is limited to 2 LED's for now. I wanted 3v to the led's so assuming they draw 10ma the formula would be 3/0.01 = 300Ohms of resistance needed.



I'm still only measuring about 1.1V across the LED's - and I don't understand why.
At the end of the resistors I'm measuring 3.3-3.9V (it's constantly moving up and down between those - is it because when my LEDs fire every 500ms the current changes so the voltage changes?)
 
Last edited:
I would suggest You, to use Low Current LED's.
They only draw 2mA current, so You can use a 2k2Ohm Resistor in series with it.
I've figured out, that the A/D Converter make Errors in the Result, when the Controller have to drive or sink higher currents.
The Effect increases with the current.
When You'll using more LED's You should use some drivers.

To calculate the needed series Resistor you have to divide the rest of voltage with the LED's current.
Eyample:
LED 1.5V ; 20mA ; Power 5V
5-1.5=3.5V
3.5V/0.02A=175Ohm
20mA is the maximum allowed Current for the LED, so select the next higher possible Resistor from E12 or E24 list.
That's 180 Ohm.
Resistors have a tolerance of maximum 20% ... 0,1% ( Better = more expensive), so Your measurement results are correct.
 
I see no capacitors on your breadboard.

Check out the application section of the 7805 datasheet. You will find that capacitors are recommended at input and output.

You should also have a 0.1uF decoupling capacitor across the supply pins of your micro-controller, as close as possible to the chip.

Things may well improve once your hardware is correct.
 
... additional the 7805 need's minimum 8V input Voltage.
So it doesn't work with the 5V USB Power.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top