Skip to content

Commit 9aaf36f

Browse files
committed
fix: wrong adc resolution for ESP32
1 parent a412b2e commit 9aaf36f

File tree

4 files changed

+59
-22
lines changed

4 files changed

+59
-22
lines changed

Diff for: examples/SerialDebug/SerialDebug.ino

+24-15
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
11
/*
2-
This file is part of the Arduino_ScienceKitCarrier library.
3-
Copyright (c) 2023 Arduino SA. All rights reserved.
2+
This file is part of the Arduino_ScienceKitCarrier library.
43
5-
This library is free software; you can redistribute it and/or
6-
modify it under the terms of the GNU Lesser General Public
7-
License as published by the Free Software Foundation; either
8-
version 2.1 of the License, or (at your option) any later version.
4+
Copyright (c) 2024 Arduino SA
95
10-
This library is distributed in the hope that it will be useful,
11-
but WITHOUT ANY WARRANTY; without even the implied warranty of
12-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13-
Lesser General Public License for more details.
14-
15-
You should have received a copy of the GNU Lesser General Public
16-
License along with this library; if not, write to the Free Software
17-
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
1810
*/
1911

12+
// This examples print all data from Arduino Science Kit R3
13+
2014
#include "Arduino_ScienceKitCarrier.h"
2115

2216
ScienceKitCarrier science_kit;
2317

2418

2519
void setup() {
2620
Serial.begin(115200);
21+
while(!Serial);
2722
science_kit.begin(START_AUXILIARY_THREADS);
2823
}
2924

@@ -93,6 +88,20 @@ void loop() {
9388
Serial.print(science_kit.getExternalTemperatureIsConnected());
9489
Serial.print("\t|| ");
9590

91+
Serial.print(science_kit.getUltrasonicIsConnected());
92+
Serial.print("\t");
93+
Serial.print(science_kit.getDistance());
94+
Serial.print("\t");
95+
Serial.print(science_kit.getTravelTime());
96+
Serial.print("\t|| ");
97+
98+
// Microphone is only availble on Arduino Nano RP2040 Connect edition
99+
#ifdef ARDUINO_NANO_RP2040_CONNECT
100+
Serial.print(science_kit.getMicrophoneRMS());
101+
Serial.print("\t|| ");
102+
#endif
103+
96104
Serial.print("\n");
105+
97106
delay(10);
98-
}
107+
}

Diff for: src/Arduino_ScienceKitCarrier.cpp

+26-6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ ScienceKitCarrier::ScienceKitCarrier(){
3434
inputA=0;
3535
inputB=0;
3636
timer_inputA = 0;
37+
board_resolution = BOARD_RESOLUTION;
3738

3839
apds9960 = new APDS9960(Wire,INT_APDS9960);
3940
proximity=0;
@@ -235,6 +236,9 @@ void ScienceKitCarrier::update(const bool roundrobin){
235236

236237

237238

239+
240+
241+
238242
/********************************************************************/
239243
/* Analog Inputs */
240244
/********************************************************************/
@@ -249,7 +253,7 @@ void ScienceKitCarrier::updateAnalogInput(const uint8_t input_to_update){
249253
if ((input_to_update==UPDATE_INPUT_A)||(input_to_update==UPDATE_ALL)){
250254

251255
if (!getExternalTemperatureIsConnected()){
252-
inputA=analogRead(inputA_pin);
256+
inputA=analogRead(inputA_pin)>>board_resolution;
253257
#ifdef ESP32
254258
beginExternalTemperature();
255259
#endif
@@ -260,7 +264,7 @@ void ScienceKitCarrier::updateAnalogInput(const uint8_t input_to_update){
260264

261265
}
262266
if ((input_to_update==UPDATE_INPUT_B)||(input_to_update==UPDATE_ALL)){
263-
inputB=analogRead(inputB_pin);
267+
inputB=analogRead(inputB_pin)>>board_resolution;
264268
}
265269
}
266270

@@ -279,12 +283,14 @@ int ScienceKitCarrier::getInputB(){
279283
/********************************************************************/
280284
/* APDS9960 */
281285
/********************************************************************/
286+
282287
int ScienceKitCarrier::beginAPDS(){
283288
if (!apds9960->begin()) {
284289
return ERR_BEGIN_APDS;
285290
}
286291
return 0;
287292
}
293+
288294
void ScienceKitCarrier::updateAPDS(){
289295
wire_lock;
290296
if (apds9960->proximityAvailable()){
@@ -407,8 +413,6 @@ float ScienceKitCarrier::getResistanceMeasureVolts(){
407413
return value;
408414
}
409415

410-
411-
412416
float ScienceKitCarrier::getResistance(){
413417
return resistance;
414418
}
@@ -590,6 +594,7 @@ float ScienceKitCarrier::getMagneticFieldZ(){
590594

591595

592596

597+
593598
/********************************************************************/
594599
/* LEDs: errors and status */
595600
/********************************************************************/
@@ -680,6 +685,9 @@ void ScienceKitCarrier::freeRTOSStatusLed(void * pvParameters){
680685
#endif
681686

682687

688+
689+
690+
683691
/********************************************************************/
684692
/* Function Generator Controller */
685693
/********************************************************************/
@@ -722,6 +730,8 @@ uint8_t ScienceKitCarrier::getRange2(){
722730

723731

724732

733+
734+
725735
/********************************************************************/
726736
/* Ultrasonic Sensor */
727737
/********************************************************************/
@@ -802,10 +812,13 @@ void ScienceKitCarrier::freeRTOSUltrasonic(void * pvParameters){
802812
}
803813
#endif
804814

815+
816+
817+
818+
805819
/********************************************************************/
806820
/* External Temperature Probe */
807821
/********************************************************************/
808-
//WIP
809822

810823
int ScienceKitCarrier::beginExternalTemperature(){
811824
new (&ow) OneWireNg_CurrentPlatform(OW_PIN, false);
@@ -854,7 +867,6 @@ void ScienceKitCarrier::updateExternalTemperature(){
854867
}
855868
}
856869

857-
858870
float ScienceKitCarrier::getExternalTemperature(){
859871
return external_temperature;
860872
}
@@ -880,9 +892,13 @@ void ScienceKitCarrier::freeRTOSExternalTemperature(void * pvParameters){
880892
#endif
881893

882894

895+
896+
897+
883898
/********************************************************************/
884899
/* Microphone */
885900
/********************************************************************/
901+
886902
#ifdef ARDUINO_NANO_RP2040_CONNECT
887903
int ScienceKitCarrier::beginMicrophone(){
888904
PDM.setGain(50);
@@ -920,6 +936,7 @@ uint ScienceKitCarrier::getMicrophoneRMS(){
920936

921937

922938

939+
923940
/********************************************************************/
924941
/* Threads */
925942
/********************************************************************/
@@ -976,6 +993,9 @@ void ScienceKitCarrier::startAuxiliaryThreads(const uint8_t auxiliary_threads){
976993
}
977994

978995

996+
997+
998+
979999
/***
9801000
* _ _
9811001
* /\ | | (_)

Diff for: src/Arduino_ScienceKitCarrier.h

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class ScienceKitCarrier{
7171

7272
uint8_t inputA_pin, inputB_pin;
7373
int inputA, inputB;
74+
uint8_t board_resolution;
7475
uint8_t timer_inputA;
7576

7677
APDS9960 * apds9960;

Diff for: src/utils/Arduino_ScienceKitCarrier_definitions.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@
2828
#define UPDATE_INPUT_A 1
2929
#define UPDATE_INPUT_B 2
3030

31+
#ifdef ARDUINO_NANO_RP2040_CONNECT
32+
#define BOARD_RESOLUTION 0
33+
#endif
34+
#ifdef ESP32
35+
#define BOARD_RESOLUTION 2
36+
#endif
37+
3138
// APDS9960
3239
#define INT_APDS9960 9
3340

@@ -113,7 +120,7 @@ const uint16_t MAXIMUM_AMPS{1}; // 1A
113120
#define START_EXTERNAL_AMBIENT_SENSOR 3 // ds18b20
114121
#define START_ULTRASONIC 4 // grove I2C ultrasonic sensor
115122
#define START_STATUS_LED 5
116-
#
123+
117124

118125
#ifdef ESP32 // 1 user, 0 secondary core
119126
#define EXTERNAL_TEMPERATURE_CORE 1

0 commit comments

Comments
 (0)