How to read a resistor value with RPI? lazarus / 21-General ---------------------------------------------------------------------- Ramses · 2021-02-04 17:28 Hi! I'm using a raspberry, and want to read the value of a resistor (like a ohmmeter). Does its possible to read the value of a resistor connected between two pin of my raspberry with lazarus? If yes can you give me an exemple of code to do that? If its not possible, which external hardware does i need to read my resistor value easily with lazarus? Thanks! ---------------------------------------------------------------------- MarkMLl · 2021-02-04 17:51 I'm really not sure that circuit design lies within the remit of this forum. You basically have two possibilities: either put a constant current through the resistor and read the voltage across it using an ADC, or set up the unknown resistor with a known high-precision resistor as a potential divider and read the voltage at the common point. However with both of these you'll find that since your ADC resolution (note: resolution, not precision) is limited, you wouldn't be able to read more than a few of the standard values (E12, E24 etc.) before you had to tell the current source to change its range or would have to change the known resistor in your potential divider. At this point you really need to think about what you're doing, since as a general solution you'd do better finding something like a general-purpose multimeter that you could interrogate over a (simulated) serial interface, or some specialised measurement module from any of the usual suspects. HTH, but this might not be the appropriate place to discuss this. MarkMLl p.s. the brand is "Raspberry Pi", not "Raspberry". ---------------------------------------------------------------------- lucamar · 2021-02-04 18:11 Quote from: MarkMLl on February 04, 2021, 06:51:38 pmp.s. the brand is "Raspberry Pi", not "Raspberry".Nitpicking and totally O.T. but, wouldn't you better say the brand is "Raspberry" and the product family "Pi", with models 1, 2, 3, etc.? Though you're right in the sense that "a Raspberry" might be either a Pi [computer/microcontroller], an IQAudio, a Pi camera, etc. ---------------------------------------------------------------------- MarkMLl · 2021-02-04 18:25 Quote from: lucamar on February 04, 2021, 07:11:02 pmQuote from: MarkMLl on February 04, 2021, 06:51:38 pmp.s. the brand is "Raspberry Pi", not "Raspberry".Nitpicking and totally O.T. but, wouldn't you better say the brand is "Raspberry" and the product family "Pi", with models 1, 2, 3, etc.? Though you're right in the sense that "a Raspberry" might be either a Pi [computer/microcontroller], an IQAudio, a Pi camera, etc. :-) Probably not, since I believe that the trading name is "The Raspberry Pi Foundation". In any event, I'm currently struggling to resist the temptation to look for rock-bottom measurement devices controlled by a serial port (possibly simulated using USB), I2C or whatever. Hopefully I've given OP enough Google fodder that he'll be able to work out why he'll have range problems etc. MarkMLl ---------------------------------------------------------------------- Ramses · 2021-02-04 18:29 Quote from: MarkMLl on February 04, 2021, 06:51:38 pmYou basically have two possibilities: either put a constant current through the resistor and read the voltage across it using an ADC, or set up the unknown resistor with a known high-precision resistor as a potential divider and read the voltage at the common point. Alright, so according to what i read, its not possible to do it directly with my "raspberry pi 3 b+" and i need to use an analog to digital converter.. Thanks for your answer, so i will order an ADC first, then when the ADC is received i will try to read my resistor value from it using lasarus.. ---------------------------------------------------------------------- MarkMLl · 2021-02-04 19:25 Quote from: Ramses on February 04, 2021, 07:29:15 pmAlright, so according to what i read, its not possible to do it directly with my "raspberry pi 3 b+" and i need to use an analog to digital converter.. Thanks for your answer, so i will order an ADC first, then when the ADC is received i will try to read my resistor value from it using lasarus.. I was assuming there was something integrated, but a quick Google suggests I was wrong. Frankly, if you're having to buy an ADC you'd be better off- IMO- looking for some sort of measurement module with some sort of range control. Most ADCs will have something like 12-bit precision, i.e. 4096 steps... now let's say you set it up so that full-scale (i.e. a reading of $0fff) is 40 kOhms your minimum reading will be 10 Ohms and that's a very small part of the range of values for which standard resistors are easily available (0.1 Ohm to 100 MOhm would be realistic, which is 10^9 i.e. roughly 30 bits). Alternatively you need to firm your ideas up and decide what range of resistor you need to measure. MarkMLl p.s. Item 333847772291 on eBay is a cheap multimeter with USB capability. It's probably good to better than 5% which is about what I'd expect a beginner to get if he were designing and building his own hardware, but I've absolutely no idea how easy it would be to interface to it. HTH, but that really is about all I can do. ---------------------------------------------------------------------- dseligo · 2021-02-04 19:58 Quote from: MarkMLl on February 04, 2021, 08:25:56 pmAlternatively you need to firm your ideas up and decide what range of resistor you need to measure. Maybe he wants to measure some NTC thermistor or some LDR and he doesn't need very wide range, so 12 bit or even 10 bit ADC will be enough for him. ---------------------------------------------------------------------- MarkMLl · 2021-02-04 20:06 Quote from: dseligo on February 04, 2021, 08:58:26 pmQuote from: MarkMLl on February 04, 2021, 08:25:56 pmAlternatively you need to firm your ideas up and decide what range of resistor you need to measure. Maybe he wants to measure some NTC thermistor or some LDR and he doesn't need very wide range, so 12 bit or even 10 bit ADC will be enough for him. Yes, maybe. But as things stand he's asked a very general question and the best we can do is give him a general answer, some Googling terms, and tell him what he needs to improve if he wants more specific advice. MarkMLl ---------------------------------------------------------------------- Ramses · 2021-02-05 00:50 Quote from: dseligo on February 04, 2021, 08:58:26 pmMaybe he wants to measure some NTC thermistor or some LDR and he doesn't need very wide range, so 12 bit or even 10 bit ADC will be enough for him. Thanks for all your reply. in fact, here is exactly why i wanna read the ohm value: i have few differents external device that i want to connect to my project (let say for exemple: 10 differents speaker), i want my "raspberry pi 3 b+" to be able to detect which speaker is connected, so i decided to use 4 wire to each speaker: wires 1 and 2 for audio, and wires 3 and 4 that have a resistor between them (it could be any value i want).  exemple:  speaker 1 have audio signal on wire 1 - 2 and have 10k resistor between wire no3 and 4  speaker 2 have audio signal on wire 1 - 2 and have 20k resistor between wire no3 and 4  speaker 3 have audio signal on wire 1 - 2 and have 30k resistor between wire no3 and 4 etc.... That way, if each speaker have a different resistor value, my "raspberry pi 3 b+" will "see" that the first one is connected if they read 10kohm for exemple, then if they read 20kohm "raspberry pi 3 b+" will know that the second speaker is connected, etc... So, for sure i dont need i very large range of detection, as long as i can read 10 different resistor (of any value) thats ok for me... ---------------------------------------------------------------------- ojz0r · 2021-02-05 06:22 Then you want an ADC with a voltage divider. Posible ADC would be MCP3208 (12-bit resolution) or even a MCP3008 (10-bit). Since precision isnt a factor you can chose the cheapest. Dont have the resistors to close to each other in value since the resistans changes with temperature. The easiest way to communicate with the ADC is with SPI or I2C. ---------------------------------------------------------------------- MarkMLl · 2021-02-05 09:01 Quote from: Ramses on February 05, 2021, 01:50:43 amSo, for sure i dont need i very large range of detection, as long as i can read 10 different resistor (of any value) thats ok for me... That's the important thing. So if you set up a potential divider with the fixed resistor being 47k (note: that's a "preferred value", see https://en.wikipedia.org/wiki/E12_series) you'd get roughly 50% of the rail voltage if your unknown were also 47k decreasing roughly in proportion with the value of your unknown. /However/, if all you're trying to do is do a 1-of-4 identification it would be easier if you assumed that one end of your speaker was grounded, at which point you could have two sense wires which were either grounded or left floating in a binary pattern. That would take 2x GPIO pins set to input with pullup resistors, leaving your I2C available for other use. MarkMLl ---------------------------------------------------------------------- ccrause · 2021-02-05 10:55 Other analog options that should be relatively cheap to build: Use 555 timer in single shot mode, can use this to measure either unknown capacitance against known resistor value, or vice versa.  Trigger 555, measure pulse width of output, resistor value is proportional to RC, so knowing the measurement capacitor, the unknown resistor value can be calculated. Or (dis)charge a cap through unknown resistor.  Send voltage through comparator to generate a signal at fixed reference voltage (or rely on the build-in threshold of the digital pins, but I expect that to be somewhat variable)  Again, resistance can be calculated from RC (dis)charge curve. Just in case the OP is interested in applying some analog domain solution... ---------------------------------------------------------------------- kupferstecher · 2021-02-05 10:59 Quote from: Ramses on February 04, 2021, 06:28:06 pmDoes its possible to read the value of a resistor connected between two pin of my raspberry with lazarus? For your intended usecase, I think its not time critical, so you could use two port pins to measure the load time of a capacitor. See schematic below. Portpin2 is the measurement input, Portpin1 starts with level low to discharge the capacitor. Then you change Portpin1 to High, the capacitor gets charged. You poll the input value of Portpin2 to measure the time between the moment you changed Portpin1 to high and the moment the input value of Portpin2 changes to high. This time is your analog value, the smaller R1 the faster C1 is loaded, the shorter your measured time. Make sure that C1 is really empty when you start the measurement. Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  Portpin1 o---------------------                               |                            ---                             |   |                            |   |  R1                             |   |                            ---                               | Portpin2 o---------------------                               |                           =====  C1                             =====                             |                               |                            ===                              GND  Edit: ccrause was faster... ---------------------------------------------------------------------- dseligo · 2021-02-05 11:45 MarkMLI suggestion is good, but if you need more than 4 speakers you'd need more wires. ccrause's and kupferstecher's solutions are fine, but are a bit complicated because they involve measuring time and calculations. You could make resistor network (R-2R) and a comparator (e.g. LM324 wired as a comparator). It would need 4 output GPIO's and one input GPIO for 16 different values. You'd go from 0 to 15 on output GPIO's and check input GPIO when comparator flip. ---------------------------------------------------------------------- MarkMLl · 2021-02-05 11:59 Quote from: dseligo on February 05, 2021, 12:45:45 pmMarkMLI suggestion is good, but if you need more than 4 speakers you'd need more wires. ccrause's and kupferstecher's solutions are fine, but are a bit complicated because they involve measuring time and calculations. You could make resistor network (R-2R) and a comparator (e.g. LM324 wired as a comparator). It would need 4 output GPIO's and one input GPIO for 16 different values. You'd go from 0 to 15 on output GPIO's and check input GPIO when comparator flip. I think we all need to bear in mind that- with respect- OP's electronics experience is limited, and we don't know what testgear he's got. I for one would be very reluctant to get involved with debugging something as complex as a 555 oscillator remotely... I've tried to throw in terms that he should be able to interpret using Wikipaedia etc. but I think that's about as much as can be reasonably done. MarkMLl ---------------------------------------------------------------------- Ramses · 2021-02-06 01:29 Quote from: MarkMLl on February 05, 2021, 10:01:36 am/However/, if all you're trying to do is do a 1-of-4 identification it would be easier if you assumed that one end of your speaker was grounded, at which point you could have two sense wires which were either grounded or left floating in a binary pattern. That would take 2x GPIO pins set to input with pullup resistors, leaving your I2C available for other use. yes my first tought was to use a binary system, but because i need to identify 10 differents speaker, it will take 6 wire (2 for audio, 4 for binary) so its a little too mutch. that why i tough about using resistor, but i was thinking that i ca mesure a resistor directly with my "raspberry pi 3b+" and its not the case.. the answer from  kupferstecher is very interesting, it dosent need a lot of part, but my coding ability is not very good on lazarus, and i will probably not be able to code that on lazarus so i'm actually looking on internet to see if i can found exemple code in lazarus to use the MCP3208 or MCP3008 ( in the answer from ojz0r) , but i have not found any exemple until now.. in fact, i have no problem to buy anything that can do what i want, and with the schematic i can build almost anything in electronic, but my problem is that i am completely beginner in lazarus, the biggest problem i have is how to interface lazarus with the electronic device.. ---------------------------------------------------------------------- ojz0r · 2021-02-06 06:19 I recently did a setup with MCP3208 and a raspberry Pi Zero that should be compatible on the 3b as well. You can have the code if you want as an example. It's based on the SPI example showed in the wiki here: https://wiki.lazarus.freepascal.org/Raspberry_Pi_-_SPI ---------------------------------------------------------------------- MarkMLl · 2021-02-06 09:23 Quote from: Ramses on February 06, 2021, 02:29:47 amyes my first tought was to use a binary system, but because i need to identify 10 differents speaker, it will take 6 wire (2 for audio, 4 for binary) so its a little too mutch. that why i tough about using resistor, but i was thinking that i ca mesure a resistor directly with my "raspberry pi 3b+" and its not the case.. OK, but in that case why didn't you give us that number MUCH earlier in the discussion? There's been a lot of work done on this sort of thing, and I'd point you at https://hackaday.com/2019/03/23/tucoplexing-a-new-charliplex- for-buttons-and-switches/ as an interesting jumping-in reference. The above mentions an I2C expander as a possibility, which does suggest that one thing that might work for you is putting something like that next to the speaker. In any event, wire length could be a problem there but you will also need to consider that if measuring a resistor... I'm not going to start lecturing on twisted pair, four- vs two-wire measurement and so on. MarkMLl ---------------------------------------------------------------------- hansotten · 2021-02-06 09:26 Get a cheap and small Arduino (328 pro mini, has 4 10 bit ADC, $3 in China), look up one of the many resistor measurement sketches for it, and sent the ADC value via serial to the Raspberry Pi to process with a Lazarus app. That way you avoid buying a most likely more expensive ADC hat for the Pi, safeguard the Pi for damage and have a very flexible solution. ---------------------------------------------------------------------- MarkMLl · 2021-02-06 09:48 Quote from: hansotten on February 06, 2021, 10:26:40 amGet a cheap and small Arduino (328 pro mini, has 4 10 bit ADC, $3 in China), look up one of the many resistor measurement sketches for it, and sent the ADC value via serial to the Raspberry Pi to process with a Lazarus app. That way you avoid buying a most likely more expensive ADC hat for the Pi, safeguard the Pi for damage and have a very flexible solution. Good point. One of the little "DigiSPARK" boards (or strictly, a clone since the originals are discontinued) would probably do nicely subject to number of pins. MarkMLl ---------------------------------------------------------------------- Ramses · 2021-02-06 13:28 Quote from: hansotten on February 06, 2021, 10:26:40 amGet a cheap and small Arduino (328 pro mini, has 4 10 bit ADC, $3 in China), look up one of the many resistor measurement sketches for it, and sent the ADC value via serial to the Raspberry Pi to process with a Lazarus app. That way you avoid buying a most likely more expensive ADC hat for the Pi, safeguard the Pi for damage and have a very flexible solution. Yes arduino is a great idea, i'm very confortable with it because i have build many device based on arduino board in the past ( security cameras, wing generator remote controler, web browser, and many other) and i already have a lot of different arduino board here (uno, uno wifi, mega2560. etc) So yes, i can read a resistor value with an arduino, so i think the easiest way is to use the arduino to identify the device according to the resistor value, then i can just past the device number (1 to 10, coded on 4 bit by using 4 pins) to my "raspberry pi 3 b+" (or maybe there is a better and easier solution to pass data that i dont know?) Now, i just need to know how to read my 4 bit data with the "raspberry pi 3 b+"? (or maybe you have a better suggestion?) ---------------------------------------------------------------------- ArtLogi · 2021-02-07 12:04 Take ie. 555 oscillator, if there is cheap IC without external parts then those or less external parts. Use different frequency for each speaker and then use Digital input channel as counter and count how many pulses you get per time unit. Digisparks are nice if you can find them with decent price vs. Delivery time. Which is not usually the case. If there would be analog input you could just add zener as voltage regulator and detect different voltage levels. Etc... ---------------------------------------------------------------------- dseligo · 2021-02-07 13:25 Quote from: Ramses on February 06, 2021, 02:28:23 pmNow, i just need to know how to read my 4 bit data with the "raspberry pi 3 b+"? (or maybe you have a better suggestion?) You can find example here: https://wiki.freepascal.org/Lazarus_on_Rasp berry_Pi#Reading_the_status_of_a_pin. ---------------------------------------------------------------------- hansotten · 2021-02-15 10:28 Quote from: Ramses on February 06, 2021, 02:28:23 pmQuote from: hansotten on February 06, 2021, 10:26:40 amGet a cheap and small Arduino (328 pro mini, has 4 10 bit ADC, $3 in China), look up one of the many resistor measurement sketches for it, and sent the ADC value via serial to the Raspberry Pi to process with a Lazarus app. That way you avoid buying a most likely more expensive ADC hat for the Pi, safeguard the Pi for damage and have a very flexible solution. Yes arduino is a great idea, i'm very confortable with it because i have build many device based on arduino board in the past ( security cameras, wing generator remote controler, web browser, and many other) and i already have a lot of different arduino board here (uno, uno wifi, mega2560. etc) So yes, i can read a resistor value with an arduino, so i think the easiest way is to use the arduino to identify the device according to the resistor value, then i can just past the device number (1 to 10, coded on 4 bit by using 4 pins) to my "raspberry pi 3 b+" (or maybe there is a better and easier solution to pass data that i dont know?) Now, i just need to know how to read my 4 bit data with the "raspberry pi 3 b+"? (or maybe you have a better suggestion?) Send to Serial, lazserial on the Raspberry side to read.